i have this code that connect to rtsp camera:
from flask import Flask, Responseimport cv2app = Flask(__name__)RTSP_URL = "rtsp://admin:xxxxxxx@x.x.x.x:554/onvif1"def generate_frames(): cap = cv2.VideoCapture(RTSP_URL) frame_skip = 2 # Skip every 2 frames while True: for _ in range(frame_skip): cap.read() # Skip frames success, frame = cap.read() if not success: break else: # Resize frame to lower resolution frame = cv2.resize(frame, (320, 240)) # Reduce to 320x240 resolution # Encode frame to JPEG format with lower quality encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 30] # Set JPEG quality to 30 _, buffer = cv2.imencode('.jpg', frame, encode_param) frame = buffer.tobytes() # Yield frame in MJPEG format yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n'+ frame + b'\r\n')@app.route('/video_feed')def video_feed(): return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
It work fine in my local pc even the camera and pc are in different network(im using ubuntu desktop) but i purchased a ubuntu server and used the same exact code and i opened the flask throguh :http://x.x.x.x:5000/video_feed and thats the ip of the ubuntu server and its very very slow even i did the same exact installation of the librareis and the harware of the server is much stronger than my ubuntu desktop,i lowered resolution and it wokred a little bit better but its still very very slow.The ubuntu server have a very better connection than my ubuntu deskotp and as i told you it have rtx 4090 24gb and 100 gb ram so its not a deal for a video stream from an rtsp camera