successful pixel receiving over windows pipes

This commit is contained in:
Nickiel12 2024-04-26 22:11:53 -07:00
parent 139acca29e
commit 57724a55ab
4 changed files with 129 additions and 2 deletions

View file

@ -33,7 +33,7 @@ def main(websocket_addr):
print("Handshake complete")
# Load YOLO model
model = YOLO('yolov8x.pt') # Load an official Detect model
model = YOLO('yolov8s.pt') # Load an official Detect model
# Open webcam
cap = cv2.VideoCapture(0)

View file

@ -0,0 +1,90 @@
import cv2
from websocket import create_connection
import win32file
import numpy as np
from ultralytics import YOLO
# object classes
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
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
model = YOLO('yolov8s.pt') # Load an official Detect model
# Open webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while cap.isOpened():
ret, frame = cap.read()
frame = cv2.resize(frame, (640, 480))
if not ret:
break
# Perform object detection
results = model.track(frame, persist=True)
for r in results:
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]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# put box in cam
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
# class name
cls = int(box.cls[0])
# object details
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)
cv2.putText(frame, str(id), org2, font, fontScale, color_w, thickness)
lines += f"{id} {x1}:{y1} {x2}:{y2}\n"
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

37
Server/PipeServer/main.py Normal file
View file

@ -0,0 +1,37 @@
import win32pipe
import win32file
from handle_connection import handle_connection
# Function to create named pipe server
def create_named_pipe_server(pipe_name):
print(f"Named pipe server started: {pipe_name}")
while True:
try:
# Create named pipe
pipe = win32pipe.CreateNamedPipe(
fr'\\.\pipe\{pipe_name}',
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
0,
None
)
win32pipe.ConnectNamedPipe(pipe, None)
handle_connection(pipe)
finally:
win32file.CloseHandle(pipe)
# Main function
if __name__ == "__main__":
# Define the name of the named pipe
pipe_name = "example_pipe"
# Create and start the named pipe server
create_named_pipe_server(pipe_name)
# Wait for user input to exit
input("Press Enter to exit...")

@ -1 +1 @@
Subproject commit ef96409082241db6ff012dc2d898894e6efde3a3
Subproject commit 506ebde9f5a96aef440b98ffe214446827ba381c