Доброго дня. Кто сможет подскажите, как реализовать программу на PyQt5. Которая сможет распозновать цвет с загружаемой картинки и выводить данные о цветах в таблицу созданную в программе.
пробовал использовать OpenCV для распознования, но не пойму как объединить с PyQt.
Open
open1
пробовал использовать OpenCV для распознования, но не пойму как объединить с PyQt.
Python:
import cv2
import numpy as np
img_path = "-5 (2).jpg"
img = cv2.imread(img_path)
resize_img = cv2.resize(img , (1500 , 700))
b = g = r = 0
clicked = False
def color_function(event, x, y, flag, param):
if event == cv2.EVENT_LBUTTONUP:
global b, r, g, clicked
b, r, g = resize_img[y, x]
b = int(b)
g = int(g)
r = int(r)
clicked = True
cv2.namedWindow("main")
cv2.setMouseCallback("main", color_function)
while True:
cv2.imshow("main", resize_img)
resize_img = cv2.resize(img, (1500, 700))
if clicked:
cv2.rectangle(img, (20, 20), (1500, 60), (b, g, r))
if b + g + r <= 500:
cv2.putText(img, "R =" + str(r) + "; G = " + str(g) + "; B = " + str (b), (200, 250), 2, 1.0, (255, 255, 255))
else:
cv2.putText(img, "R =" + str(r) + "; G = " + str(g) + "; B = " + str(b), (200, 250), 2, 1.0, (0, 0, 0))
clicked = False
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
Open
Python:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem, QWidget
from PyQt5.QtGui import QPixmap, QImage
from PIL import Image
import cv2
from open1 import Ui_Photo_view
class show_photo(QMainWindow):
def __init__(self):
super(show_photo, self).__init__()
self.ui = Ui_Photo_view()
self.ui.setupUi(self)
self.view = QGraphicsView(self)
self.view.setGeometry(0, 0, 710, 650)
def select_button_clicked(self):
file_name = QFileDialog.getOpenFileName(self, "Open File", "./", "jpg (*.jpg)")
image_path = file_name[0]
if (file_name[0] == ""):
QMessageBox.information(self, "Prompt", self.tr("No picture file selected!"))
print(image_path)
img = cv2.imread(image_path) # read image
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# Convert image channel
img = cv2.resize(img, (1920, 1080))
x = img.shape[1] # Get image size
y = img.shape[0]
self.zoomscale = 1 # Image zoom scale
frame = QImage(img, x, y, QImage.Format_RGB888)
pix = QPixmap.fromImage(frame)
self.item = QGraphicsPixmapItem(pix) # Create pixel image element
self.scene = QGraphicsScene() # Create a scene
self.scene.addItem(self.item)
self.view.setScene(self.scene)
self.view.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = show_photo()
mainWindow.show()
sys.exit(app.exec_())
open1
Python:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget
class Ui_Photo_view(QWidget):
def setupUi(self, Photo_view):
Photo_view.setObjectName("Photo_view")
Photo_view.resize(1000, 800)
self.centralwidget = QtWidgets.QWidget(Photo_view)
self.centralwidget.setObjectName("centralwidget")
self.select_button = QtWidgets.QPushButton(self.centralwidget)
self.select_button.setGeometry(QtCore.QRect(770, 140, 75, 23))
self.select_button.setCheckable(True)
self.select_button.setObjectName("select_button")
self.pic_show = QtWidgets.QGraphicsView(self.centralwidget)
self.pic_show.setGeometry(QtCore.QRect(0, 0, 711, 641))
self.pic_show.setObjectName("pic_show")
Photo_view.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Photo_view)
self.menubar.setGeometry(QtCore.QRect(0, 0, 890, 23))
self.menubar.setObjectName("menubar")
Photo_view.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Photo_view)
self.statusbar.setObjectName("statusbar")
Photo_view.setStatusBar(self.statusbar)
self.retranslateUi(Photo_view)
self.select_button.clicked.connect(Photo_view.select_button_clicked)
QtCore.QMetaObject.connectSlotsByName(Photo_view)
def retranslateUi(self, Photo_view):
_translate = QtCore.QCoreApplication.translate
Photo_view.setWindowTitle(_translate("Photo_view", "MainWindow"))
self.select_button.setText(_translate("Photo_view", "Select File"))
Последнее редактирование: