I’m trying to write a program that transfers realtime video from seek thermal integrated with raspberry pi 3b+, flask server with websocket protocol and PC, where the raspberry pi acts as a client that captures and streams the realtime frames to the flask server, and the pc acts as a server that receive frames and apply people detection using yolov8 AI model to them before displaying them, but unfortunately when i try to run the code on both raspberry pi and pc i get connection refused, can someone help me please?
**the client code: **
import cv2
import numpy as np
from threading import Condition
import websocket
from seekcamera import (
SeekCameraIOType,
SeekCameraColorPalette,
SeekCameraManager,
SeekCameraManagerEvent,
SeekCameraFrameFormat,
SeekCamera,
SeekFrame,
)
class Renderer:
def _init_(self):
self.busy = False
self.frame = SeekFrame()
self.camera = SeekCamera()
self.frame_condition = Condition()
self.first_frame = True
def on_frame(_camera, camera_frame, renderer):
with renderer.frame_condition:
renderer.frame = camera_frame.color_argb8888
renderer.frame_condition.notify()
def on_event(camera, event_type, event_status, renderer):
print("{}: {}".format(str(event_type), camera.chipid))
if event_type == SeekCameraManagerEvent.CONNECT:
if renderer.busy:
return
renderer.busy = True
renderer.camera = camera
renderer.first_frame = True
camera.color_palette = SeekCameraColorPalette.WHITE_HOT
camera.register_frame_available_callback(on_frame, renderer)
camera.capture_session_start(SeekCameraFrameFormat.COLOR_ARGB8888)
elif event_type == SeekCameraManagerEvent.DISCONNECT:
if renderer.camera == camera:
camera.capture_session_stop()
renderer.camera = None
renderer.frame = None
renderer.busy = False
elif event_type == SeekCameraManagerEvent.ERROR:
print("{}: {}".format(str(event_status), camera.chipid))
def stream_video(renderer):
ws = websocket.create_connection("ws://127.0.0.1:8080/stream")
while True:
with renderer.frame_condition:
renderer.frame_condition.wait(150.0 / 1000.0)
frame = renderer.frame
ws.send_binary(frame)
ws.close()
if _name_ == "_main_":
renderer = Renderer()
with SeekCameraManager(SeekCameraIOType.USB) as manager:
manager.register_event_callback(on_event, renderer)
# Stream video to Flask server
stream_video(renderer)
**the server code: **
from flask import Flask
import cv2
import numpy as np
from ultralytics import YOLO
import supervision as sv
#code for people detection on pc
app = Flask(name)
model = YOLO('yolov8n.pt')
@app.route('/stream')
def stream():
return 'Streaming...'
def processframe(frame):
while True:
# Convert frame to OpenCV format
frame = cv2.imdecode(np.frombuffer(frame, np.uint8), cv2.IMREADCOLOR)
# Perform people detection using YOLOv8
results = model(frame)[0]
detections = sv.Detections.fromultralytics(results)
# Annotate the frame
labelannotator = sv.LabelAnnotator(
border_radius=1,
text_thickness=2,
text_scale=1
)
annotated_frame = label_annotator.annotate(scene=frame, detections=detections)
# Display the annotated frame
cv2.imshow('Seek Thermal Camera', annotated_frame)
cv2.waitKey(1)
if _name == "_main":
import websocket
ws = websocket.WebSocketApp("ws://192.168.1.8:8080/stream",
on_message=lambda ws, message: print(f"Received message: {message}"),
on_error=lambda ws, error: print(f"Error: {error}"),
on_close=lambda ws, close_status_code, close_reason: print(f"Connection closed: {close_status_code} - {close_reason}"))
ws.run_forever()
I tried those codes as a client and server, and this error occurs “Errno 111 connection refused”.
Nancy Mohammed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.