Skip to content

Commit

Permalink
feat: splitting streaming from frontapp and performance detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziad-Muhammed committed Sep 29, 2022
1 parent 97daa79 commit 58c344a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 219 deletions.
97 changes: 1 addition & 96 deletions FrontApp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import sys

import cv2 as cv
from PIL import Image, ImageQt
from PyQt5 import QtWidgets, uic, QtGui, QtCore
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QPushButton, QTextEdit, QLabel

# from detect import crop_one
Expand All @@ -21,7 +18,7 @@
import glob

btn_pushed = False
frame =0
frame = 0


class UI(QMainWindow):
Expand All @@ -36,28 +33,14 @@ def __init__(self):
self.findChild(QPushButton, "startButton").clicked.connect(self.Run)
# Get Input from each field
self.input_path = self.findChild(QTextEdit, "inputPath")
# self.input_path = self.input_path.toPlainText()
# self.output_path = self.findChild(QTextEdit,"outputPath")
# self.output_path = self.output_path.toPlainText()
# self.findChild(QPushButton,'saveButton').clicked.connect(self.saveImg)
self.findChild(QPushButton, 'skipButton').clicked.connect(self.skipImg)
# QtCore.QObject.connect(save_btn,QtCore.SIGNAL("clicked()"),self.saveImg)
# QtCore.QObject.connect(skip_btn,QtCore.SIGNAL("clicked()"),self.skipImg)
# self.function = self.findChild(QLineEdit, "Function")
# self.minVal = self.findChild(QLineEdit, "Min")
self.main_img = self.findChild(QLabel, "mainImage")
self.main_img.hide()
self.plate_img = self.findChild(QLabel, "plateImg")
self.green_img = self.findChild(QLabel, "greenImg")
self.green_img.hide()
self.endpoint = self.findChild(QLabel, "endpoint")
self.Lp = self.findChild(QLabel, "LP")
self.stream = self.findChild(QLabel, "stream")
self.capturebtn = self.findChild(QPushButton, "capture").clicked.connect(VideoThread.captureImg)
self.findChild(QPushButton, "close").clicked.connect(self.shutDown)
self.findChild(QPushButton, "PushStream").clicked.connect(self.streaming)
# self.capturebtn.hide()
self.stream.hide()
self.endpoint.hide()
self.Lp.hide()
self.plate_img.hide()
Expand Down Expand Up @@ -139,50 +122,11 @@ def clean_directory(self, path):
files = glob.glob(f'{path}/*')
for file in files:
os.remove(file)

def streaming(self):
# self.capturebtn.show()
self.stream.show()
self.thread = VideoThread()
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()

def shutDown(self):
self.stream.hide()
# self.capturebtn.hide()

def closeEvent(self, event):
self.thread.stop()
event.accept()

@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
self.stream.setPixmap(qt_img)

def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv.cvtColor(cv_img, cv.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(531, 331, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)

def clean(self):
for label, text in zip(self.segmented_chars, self.textbox_values):
label.clear()
text.setText("0")

# def create_directories(self):
# output_path = self.output_path.toPlainText()
# for i in range(1,27):
# if not os.path.exists(output_path+f"/{i}"):
# os.mkdir(output_path+f"/{i}")

def saveImg(self):
print("Saved")
mappings = {
Expand Down Expand Up @@ -236,45 +180,6 @@ def skipImg(self):
self.clean()


class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)

def __init__(self):
super().__init__()
self._run_flag = True
# self.frame = 0

def run(self):
# capture from web-camera
capture = cv.VideoCapture(0)
while self._run_flag:
global frame
ret, frame = capture.read()
if ret:
self.change_pixmap_signal.emit(frame)

# if cv.waitKey(1) == ord('q'):
# cv.imwrite(path, frame)
# break

# shut down capture system
capture.release()

def stop(self):

"""Sets run flag to False and waits for thread to finish"""
self._run_flag = False
self.wait()

def captureImg(self):
try:
# cv.imshow('demo', frame)
global frame
cv.imwrite(path, frame)
except Exception:
print("Errorr")


# Main
if __name__ == "__main__":
application = QtWidgets.QApplication(sys.argv)
Expand Down
106 changes: 8 additions & 98 deletions frontapp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1525</width>
<width>1119</width>
<height>843</height>
</rect>
</property>
Expand Down Expand Up @@ -133,8 +133,8 @@ padding: 5px;
<widget class="QLabel" name="plateImg">
<property name="geometry">
<rect>
<x>500</x>
<y>470</y>
<x>430</x>
<y>480</y>
<width>151</width>
<height>71</height>
</rect>
Expand All @@ -153,8 +153,8 @@ padding:3px</string>
<widget class="QLabel" name="endpoint">
<property name="geometry">
<rect>
<x>770</x>
<y>580</y>
<x>680</x>
<y>570</y>
<width>261</width>
<height>51</height>
</rect>
Expand Down Expand Up @@ -194,8 +194,8 @@ padding: 5px;
<widget class="QLabel" name="LP">
<property name="geometry">
<rect>
<x>180</x>
<y>580</y>
<x>90</x>
<y>570</y>
<width>261</width>
<height>51</height>
</rect>
Expand All @@ -211,103 +211,13 @@ border: 2px solid #86bcfa;
<string/>
</property>
</widget>
<widget class="QLabel" name="stream">
<property name="geometry">
<rect>
<x>1030</x>
<y>150</y>
<width>431</width>
<height>291</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-radius: 8px;
background-color: white;
color: black;
border: 2px solid #86bcfa;
padding: 5px;
</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="PushStream">
<property name="geometry">
<rect>
<x>1300</x>
<y>490</y>
<width>161</width>
<height>51</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-radius: 8px;
font-size: 20px;
background-color: #3093ed;
color: black;
border: 2px solid #3093ed;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
</string>
</property>
<property name="text">
<string>Stream</string>
</property>
</widget>
<widget class="QPushButton" name="close">
<property name="geometry">
<rect>
<x>1100</x>
<y>490</y>
<width>161</width>
<height>51</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-radius: 8px;
font-size: 20px;
background-color: #3093ed;
color: black;
border: 2px solid #3093ed;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
</string>
</property>
<property name="text">
<string>Close Stream</string>
</property>
</widget>
<widget class="QPushButton" name="capture">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>1200</x>
<y>580</y>
<width>161</width>
<height>51</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-radius: 8px;
font-size: 20px;
background-color: #3093ed;
color: black;
border: 2px solid #3093ed;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
</string>
</property>
<property name="text">
<string>Capture</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1525</width>
<width>1119</width>
<height>26</height>
</rect>
</property>
Expand Down
2 changes: 1 addition & 1 deletion localisation/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def crop_multiple(directory_path, detect_multiple=False, saved_model_loaded=Fals
image_paths = []
file_paths = os.listdir(directory_path)
image_paths = [(os.path.join(directory_path, path)).replace(os.sep, '/') for path in file_paths if path.endswith(image_extensions)]
final_crop_paths = [detect_and_crop(image_path, saved_model_loaded, detect_multiple) for image_path in image_paths]
# final_crop_paths = [detect_and_crop(image_path, saved_model_loaded, detect_multiple) for image_path in image_paths]
for image in image_paths:
crop_path = detect_and_crop(image, saved_model_loaded, detect_multiple)
if crop_path is not None:
Expand Down
11 changes: 8 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from recognition.KNN import predictChars

path = './outputs/'
# url = "http://192.168.98.146:8080/video"
url = "http://9.246.91.33:8080/video"
# url = "http://192.168.20.51:8080/video"
url = "http://192.168.20.51:8080/video"
# url = "http://9.246.91.33:8080/video"
sys.path.insert(0, './localisation')
import detect as detect
from localisation.core.functions import load_model
Expand Down Expand Up @@ -49,6 +50,7 @@ def printChars(chars, count):
# segmentation
# _, chars = segmentChars(filename.path)
chars = newOldSegmentation(filename.path)
# chars = segmentCharacters(filename.path)
# print chars segmented
printChars(chars, countPlate)

Expand All @@ -60,7 +62,7 @@ def printChars(chars, count):
lp = predictChars(predicted_chars)
print(lp)
print(end - start)
# countPlate += 1
countPlate += 1

# send string to middle-ware
print(endPoint(lp))
Expand All @@ -70,6 +72,9 @@ def printChars(chars, count):
files = glob.glob('./outputs/1/*')
for file in files:
os.remove(file)
files=glob.glob('./green_boxes/*')
for file in files:
os.remove(file)

except Exception:
# print("error")
Expand Down
4 changes: 2 additions & 2 deletions stream.ui
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
</property>
<property name="geometry">
<rect>
<x>490</x>
<y>680</y>
<x>430</x>
<y>670</y>
<width>161</width>
<height>51</height>
</rect>
Expand Down
Loading

0 comments on commit 58c344a

Please sign in to comment.