-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyolov10.py
57 lines (45 loc) · 1.54 KB
/
pyolov10.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
from ultralytics import YOLOv10
import supervision as sv
import cv2
import socket
import pickle
MODEL_PATH = 'YOUR YOLOV10 PT'
cap = cv2.VideoCapture(0)
model = YOLOv10(MODEL_PATH)
bounding_box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()
# create TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 5000))
server_socket.listen(1)
print("Waiting for D app to connect...")
conn, addr = server_socket.accept()
print(f"{addr} connected!")
while True:
ret, frame = cap.read()
if not ret:
print("Video Error")
break
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
annotated_image = bounding_box_annotator.annotate(scene=frame, detections=detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)
# get information of detected objects
data = []
for i in range(len(detections.class_id)):
item = {
"class_id": detections.class_id[i],
"confidence": detections.confidence[i],
"bbox": detections.xyxy[i].tolist() # bounding box coordinates
}
data.append(item)
serialized_data = pickle.dumps(data)
conn.sendall(serialized_data)
cv2.imshow("Yolov10", annotated_image)
k = cv2.waitKey(1)
if k % 256 == 27: # press ESC for exit
print("Closing...")
break
cap.release()
conn.close()
cv2.destroyAllWindows()