working windows-pipe duplex communication
This commit is contained in:
parent
57724a55ab
commit
fc73f8e0a3
2 changed files with 56 additions and 56 deletions
|
@ -2,6 +2,7 @@
|
||||||
import cv2
|
import cv2
|
||||||
from websocket import create_connection
|
from websocket import create_connection
|
||||||
import win32file
|
import win32file
|
||||||
|
import struct
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
@ -20,71 +21,70 @@ classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "trai
|
||||||
|
|
||||||
|
|
||||||
def handle_connection(pipe):
|
def handle_connection(pipe):
|
||||||
print("Pipe connected!")
|
|
||||||
data = win32file.ReadFile(pipe, 640*480*3)[1]
|
|
||||||
|
|
||||||
if not data:
|
|
||||||
return
|
|
||||||
|
|
||||||
nparr = np.frombuffer(data, np.uint8)
|
|
||||||
|
|
||||||
nparr = nparr.reshape((480, 640, 3))
|
|
||||||
|
|
||||||
# frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
|
||||||
frame = cv2.cvtColor(nparr, cv2.COLOR_BGR2RGB)
|
|
||||||
|
|
||||||
raise ValueError("There is no value running this application further")
|
|
||||||
|
|
||||||
# Load YOLO model
|
# Load YOLO model
|
||||||
model = YOLO('yolov8s.pt') # Load an official Detect model
|
model = YOLO('yolov8s.pt') # Load an official Detect model
|
||||||
|
|
||||||
# Open webcam
|
print("Pipe connected!")
|
||||||
cap = cv2.VideoCapture(0)
|
try:
|
||||||
cap.set(3, 640)
|
while True:
|
||||||
cap.set(4, 480)
|
data = win32file.ReadFile(pipe, 640*480*3)[1]
|
||||||
|
|
||||||
while cap.isOpened():
|
if not data:
|
||||||
ret, frame = cap.read()
|
return
|
||||||
frame = cv2.resize(frame, (640, 480))
|
|
||||||
if not ret:
|
|
||||||
break
|
|
||||||
|
|
||||||
# Perform object detection
|
nparr = np.frombuffer(data, np.uint8).reshape((480, 640, 3))
|
||||||
results = model.track(frame, persist=True)
|
|
||||||
|
|
||||||
for r in results:
|
# frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||||
lines = ""
|
frame = cv2.cvtColor(nparr, cv2.COLOR_BGR2RGB)
|
||||||
boxes = r.boxes
|
|
||||||
for box in boxes:
|
|
||||||
if box.cls[0].item() == 0 and not box.id is None:
|
|
||||||
# bounding box
|
|
||||||
id = box.id.int().cpu().tolist()
|
|
||||||
x1, y1, x2, y2 = box.xyxy[0]
|
|
||||||
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
|
|
||||||
|
|
||||||
# put box in cam
|
# Perform object detection
|
||||||
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
|
results = model.track(frame, persist=True)
|
||||||
|
|
||||||
# class name
|
for r in results:
|
||||||
cls = int(box.cls[0])
|
lines = ""
|
||||||
|
boxes = r.boxes
|
||||||
|
for box in boxes:
|
||||||
|
if box.cls[0].item() == 0 and not box.id is None:
|
||||||
|
# bounding box
|
||||||
|
id = box.id.int().cpu().tolist()
|
||||||
|
# x1, y1, x2, y2 = box.xyxy[0]
|
||||||
|
# # 2.08333 = 1/480 * 1000 or normalize, then save 4 sig-figures and cast to int
|
||||||
|
# # 1.5625 = 1/640 * 1000 or normalize, then save 4 sig-figures and cast to int
|
||||||
|
|
||||||
# object details
|
# x1, x2, y1, y2 = int(x1), int(x2), int(y1), int(y2)
|
||||||
org = [x1, y1]
|
|
||||||
org2 = [x1, y1+50]
|
|
||||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
|
||||||
fontScale = 1
|
|
||||||
color = (255, 0, 0)
|
|
||||||
color_w = (255, 255, 255)
|
|
||||||
thickness = 2
|
|
||||||
|
|
||||||
cv2.putText(frame, classNames[cls], org, font, fontScale, color, thickness)
|
# # put box in cam
|
||||||
cv2.putText(frame, str(id), org2, font, fontScale, color_w, thickness)
|
# cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
|
||||||
|
|
||||||
lines += f"{id} {x1}:{y1} {x2}:{y2}\n"
|
# # class name
|
||||||
|
# cls = int(box.cls[0])
|
||||||
|
|
||||||
cv2.imshow('Webcam', frame)
|
# # object details
|
||||||
if cv2.waitKey(1) == ord('q'):
|
# org = [x1, y1]
|
||||||
break
|
# org2 = [x1, y1+50]
|
||||||
|
# font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
|
# fontScale = 1
|
||||||
|
# color = (255, 0, 0)
|
||||||
|
# color_w = (255, 255, 255)
|
||||||
|
# thickness = 2
|
||||||
|
|
||||||
cap.release()
|
# cv2.putText(frame, classNames[cls], org, font, fontScale, color, thickness)
|
||||||
cv2.destroyAllWindows()
|
# cv2.putText(frame, str(id), org2, font, fontScale, color_w, thickness)
|
||||||
|
|
||||||
|
x1, y1, x2, y2 = box.xyxyn[0]
|
||||||
|
x1, y1, x2, y2 = int(x1 * 1000), int(y1 * 1000), int(x2 * 1000), int(y2 * 1000)
|
||||||
|
lines += f"{id} {x1}:{y1} {x2}:{y2}\n"
|
||||||
|
|
||||||
|
# cv2.imshow('Webcam', frame)
|
||||||
|
# if cv2.waitKey(1) == ord('q'):
|
||||||
|
# break
|
||||||
|
|
||||||
|
ret = lines.encode('utf-8')
|
||||||
|
length_prefix = struct.pack('I', len(ret))
|
||||||
|
|
||||||
|
win32file.WriteFile(pipe, length_prefix+ret)
|
||||||
|
except Exception as e:
|
||||||
|
print("Pipe closed with error: " + str(e))
|
||||||
|
return
|
||||||
|
# finally:
|
||||||
|
# cv2.destroyAllWindows()
|
|
@ -1 +1 @@
|
||||||
Subproject commit 506ebde9f5a96aef440b98ffe214446827ba381c
|
Subproject commit 3cb560093c6de5794cb654a46fb6dd12c8377042
|
Loading…
Reference in a new issue