From acfa58f6651cf65a21858b1ba90cc2a9b1296bdd Mon Sep 17 00:00:00 2001 From: mht Date: Sun, 9 Mar 2025 14:24:27 +0330 Subject: [PATCH] added auto ip finding and changed gst pipeline --- core.py | 9 +++++---- server.py | 55 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/core.py b/core.py index 7e9e1a8..6ff58bd 100755 --- a/core.py +++ b/core.py @@ -18,10 +18,10 @@ import time from PyQt5.QtCore import QObject, pyqtSignal import ctypes from ctypes import c_int64 -from server import rtsp_server , run_server +from server import rtsp_server, run_server, RTSPServer -showTrack = True +showTrack = False class Core(QThread): newFrame = pyqtSignal(object, int, bool, ctypes.c_int64) @@ -65,8 +65,9 @@ class Core(QThread): self.__processing_id = source_id def set_video_sources(self, video_sources): - self.__video_sources = video_sources - self.set_source(0) + if len(video_sources) >= 2: + self.__video_sources = video_sources + self.set_source(0) def __detection(self): while self.__is_detecting: diff --git a/server.py b/server.py index bdca903..8e34963 100644 --- a/server.py +++ b/server.py @@ -6,10 +6,30 @@ import numpy as np gi.require_version('Gst', '1.0') gi.require_version('GstRtspServer', '1.0') from gi.repository import Gst, GstRtspServer, GLib +import socket + +def get_local_ip(): + try: + # Create a socket object + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + + # Connect to an external server (doesn't actually send any data) + s.connect(("8.8.8.8", 80)) + + # Get the local IP address + local_ip = s.getsockname()[0] + + # Close the socket + s.close() + + return local_ip + except Exception as e: + return str(e) + class VideoStream(GstRtspServer.RTSPMediaFactory): - def __init__(self, fps=30, width=640, height=480): + def __init__(self, fps=25, width=1920, height=1080): super(VideoStream, self).__init__() self.fps = fps self.width = width @@ -40,10 +60,11 @@ class VideoStream(GstRtspServer.RTSPMediaFactory): "appsrc name=source is-live=true format=GST_FORMAT_TIME " "caps=video/x-raw,format=RGB,width={},height={},framerate={}/1 " "! videoconvert ! video/x-raw,format=I420 " - "! x264enc tune=zerolatency bitrate=500 speed-preset=ultrafast " - "! rtph264pay config-interval=1 name=pay0 pt=96" + "! nvh264enc preset=low-latency rc-mode=cbr bitrate=2500 gop-size=7 zerolatency=true " + "! h264parse ! rtph264pay config-interval=1 name=pay0 pt=96" ).format(self.width, self.height, self.fps) + pipeline = Gst.parse_launch(pipeline_str) src = pipeline.get_by_name("source") src.connect("need-data", self.on_need_data) @@ -71,21 +92,21 @@ class RTSPServer: self.video_stream.update_frame(frame) # Global server instance -rtsp_server = RTSPServer("192.168.0.196",41231) +rtsp_server = RTSPServer(get_local_ip(),41231) def run_server(): rtsp_server.start() -def stream_webcam(): - cap = cv2.VideoCapture("/home/mht/Downloads/bcd2890d71caaf0e095b95c9b525973f61186656-360p.mp4") # Open webcam - while cap.isOpened(): - ret, frame = cap.read() - if ret: - rtsp_server.update_frame(frame) # Send frame to RTSP server - -if __name__ == "__main__": - # Start RTSP server in a separate thread - threading.Thread(target=run_server, daemon=True).start() - - # Stream webcam frames - stream_webcam() +# def stream_webcam(): +# cap = cv2.VideoCapture("/home/mht/Downloads/bcd2890d71caaf0e095b95c9b525973f61186656-360p.mp4") # Open webcam +# while cap.isOpened(): +# ret, frame = cap.read() +# if ret: +# rtsp_server.update_frame(frame) # Send frame to RTSP server + +# if __name__ == "__main__": +# # Start RTSP server in a separate thread +# threading.Thread(target=run_server, daemon=True).start() +# +# # Stream webcam frames +# stream_webcam()