forked from JungLearnBot/RPi5_yolov8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_picam_coral_tpu.py
135 lines (107 loc) · 4.67 KB
/
main_picam_coral_tpu.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
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap
import sys
import cv2
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread
import numpy as np
from picamera2 import Picamera2
from yolo_manager import YoloDetectorWrapper
from utils import SimpleFPS, draw_fps, draw_annotation
import argparse
import time
class VideoThreadPiCam(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
self.grab_frame = True
def run(self):
# capture from web cam
picam2 = Picamera2()
# camera_config = picam2.create_still_configuration(main={"size": (640, 480)}, lores={"size": (640, 480)}, display="lores")
camera_config = picam2.create_video_configuration(main={"size":(640,480),"format":"RGB888"}, raw={"size": (640, 480)})
picam2.configure(camera_config)
picam2.start()
while True:
if self.grab_frame:
frame = picam2.capture_array()
self.change_pixmap_signal.emit(frame)
self.grab_frame = False
else:
time.sleep(0.0001)
class App(QWidget):
def __init__(self, camera_test_only, use_coral_tpu):
super().__init__()
self.camera_test_only = camera_test_only
if camera_test_only:
self.yolo_detector = None
else:
self.yolo_detector = YoloDetectorWrapper(args.model, use_coral_tpu)
self.setWindowTitle("Qt UI")
self.disply_width = 640
self.display_height = 480
# create the label that holds the image
self.image_label = QLabel(self)
self.image_label.resize(self.disply_width, self.display_height)
self.fps_util = SimpleFPS()
# create a vertical box layout and add the two labels
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
self.setLayout(vbox)
# create the video capture thread
self.thread = VideoThreadPiCam()
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
# cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
if self.yolo_detector is None:
display_img = cv_img
else:
results = self.yolo_detector.predict(cv_img)
display_img = draw_annotation(cv_img, self.yolo_detector.get_label_names(), results)
fps, _ = self.fps_util.get_fps()
draw_fps(display_img, fps)
qt_img = self.convert_cv_qt(display_img)
self.image_label.setPixmap(qt_img)
self.thread.grab_frame = True
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
# rgb_image = cv_img
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(self.disply_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
def run_no_ui(use_coral_tpu):
# capture from web cam
yolo_detector = YoloDetectorWrapper(args.model, use_coral_tpu)
picam2 = Picamera2()
camera_config = picam2.create_video_configuration(main={"size":(640,480),"format":"RGB888"}, raw={"size": (640, 480)})
picam2.configure(camera_config)
picam2.start()
fps_util = SimpleFPS()
while True:
frame = picam2.capture_array()
results = yolo_detector.predict(frame)
fps, is_updated = fps_util.get_fps()
if is_updated:
print(fps)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="./models/yolov8n_full_integer_quant_edgetpu_192x192.tflite")
parser.add_argument('--camera_test', action=argparse.BooleanOptionalAction)
parser.add_argument('--debug', action=argparse.BooleanOptionalAction, default=True)
parser.add_argument('--coral_tpu', action=argparse.BooleanOptionalAction, default=True)
args = parser.parse_args()
if args.debug or args.camera_test:
app = QApplication(sys.argv)
a = App(camera_test_only=args.camera_test, use_coral_tpu=args.coral_tpu)
a.show()
sys.exit(app.exec_())
else:
run_no_ui(args.coral_tpu)