-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsm3ny.py
186 lines (155 loc) · 6.97 KB
/
Esm3ny.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- 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_())