# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'Esmanii.ui' # # Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from bidi.algorithm import get_display import arabic_reshaper from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog from PyQt5.QtGui import QImage, QTextCursor import imutils import time import numpy as np import torch import cv2 from PIL import Image from utils.plots import Annotator, colors, save_one_box class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Esm3ny") Dialog.resize(1121, 853) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(1010, 690, 93, 71)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Dialog) self.pushButton_2.setGeometry(QtCore.QRect(1010, 770, 93, 71)) self.pushButton_2.setObjectName("pushButton_2") self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(14, 15, 1091, 641)) self.label.setLayoutDirection(QtCore.Qt.RightToLeft) self.label.setAutoFillBackground(False) self.label.setText("") self.label.setPixmap(QtGui.QPixmap("logo-color_0.png")) self.label.setObjectName("label") self.textBrowser = QtWidgets.QTextBrowser(Dialog) self.textBrowser.setGeometry(QtCore.QRect(20, 690, 981, 151)) self.textBrowser.setObjectName("textBrowser") self.textBrowser.setStyleSheet('font-size: 30px;') self.retranslateUi(Dialog) self.pushButton.clicked.connect(self.load_image) # type: ignore self.pushButton_2.clicked.connect(self.clear_global_word) # type: ignore QtCore.QMetaObject.connectSlotsByName(Dialog) self.filename =None self.temp = None self.started = False self.global_word = " " def clear_global_word(self): self.textBrowser.clear() self.global_word = " " def load_image(self): if self.started: self.started=False self.pushButton.setText('Start') else: self.started=True self.pushButton.setText('Stop') model_name = 'best_final_2.onnx' model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_name) model.conf = 0.65 model.max_det = 1 cap = cv2.VideoCapture(0) cls0 = None is_global_word_edited = False num = 1 # my editing num2 = 0 # my editingy # global_word = " " clearing_flag = 1 # self.text_browser.append("hello") while (cap.isOpened()): ret, self.image = cap.read() image = Image.fromarray(cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)) results = model(image) var2 = results.xyxy[0].tolist() var = str(results.tolist()[0]).split()[4] #var = var.replace("(", "").replace(")", "") print("results",len(var2)) # if num == 1 and len(var2) > 0: # if there is one prediction and num = 0 cls0 = var2[0][-1] print("cls0",cls0) labels = results.names annotator = Annotator(self.image, line_width=3, example=str(labels)) boxes = results.xyxy[0].tolist() for box in boxes: x1, y1, x2, y2, confidence, class_id = box xyxy = (int(x1), int(y1), int(x2), int(y2)) label = labels[int(class_id)] # Reshape the Arabic text correctly reshaped_label = arabic_reshaper.reshape(label) label_corrected = get_display(reshaped_label) annotator.box_label(xyxy, label_corrected, color=colors(int(class_id),True)) if int(class_id) != int(cls0) or len(self.global_word) == 1: if label == 'أنا' and len(self.global_word) == 1 : self.global_word = "مرحبا، " + self.global_word if label == 'اسمي' or label == 'بحبك' or label == 'عمري' or label == 'أنا': self.global_word = self.global_word +" "+ label else: self.global_word = self.global_word + label cls0 = class_id # print("class_id is updated") num2 += 1 self.textBrowser.clear() is_global_word_edited = True # print('class_id new :', class_id) # print('class_id new :', label) # my editing import datetime if num2 == 6 or label == 'اسمي' or label == 'بحبك' or label == 'عمري' or label == 'أنا': self.global_word = self.global_word + " " self.textBrowser.clear() is_global_word_edited = True print(type(self.global_word)) if is_global_word_edited: is_global_word_edited = False self.textBrowser.moveCursor(QTextCursor.Start) self.textBrowser.append(" ".join(self.global_word.split())) self.image = annotator.result() self.setPhoto(self.image) # cv2.imshow('Object Detection', self.image) key = cv2.waitKey(1) & 0xFF if self.started == False: self.image=cv2.imread("Screenshot 2023-05-08 214249.png") self.setPhoto(self.image) break print('Loop break') # Release the capture and close the windows cap.release() cv2.destroyAllWindows() def setPhoto(self, image): """ This function will take image input and resize it only for display purpose and convert it to QImage to set at the label. """ self.tmp = image image = imutils.resize(image, width=1121, height=853) frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888) self.label.setPixmap(QtGui.QPixmap.fromImage(image)) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Esm3ny")) self.pushButton.setText(_translate("Dialog", "Start")) self.pushButton_2.setText(_translate("Dialog", "Clear")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())