Search code examples
pythonopencvrtsp

Can't use opencv to read my ip camera video


Hello I am a beginner on opencv and I am trying to create an real time object detection program through a HIKVISION IP camera. Using RTSP but when I run the code I get this error

Ip_add is like rtsp://login:password@ip_address:554/streaming/channels/101

cap = cv2.VideoCapture(Ip_add, cv2.CAP_FFMPEG)

while True:
_, frame = cap.read()
frame = cv2.resize(frame, dsize=(1400, 600))

(class_ids, scores, bboxes) = model.detect(frame)

for class_id, score, bbox in zip(class_ids, scores, bboxes):
    (x, y, w, h) = bbox
    cv2.putText(frame, classes[class_id], (x, y - 10), cv2.FONT_HERSHEY_PLAIN, 2,(200, 0, 50),2)
    cv2.rectangle(frame, (x, y), (x + w, y + h), (200, 0, 50), 2)
cv2.imshow("IP Camera", frame)
if cv2.waitKey(1) == ord("q"):
    break
cap.release()
cv2.destroyAllWindows()

Can someone please help me ?


Solution

  • I finaly solved the problem by using imutils instead of opencv to read the real time video stream.

    import cv2
    import imutils
    from imutils.video import VideoStream
    
    rtsp_url = "rtsp://login:passzord@ipaddress/streaming/channels/101"
    video_stream = VideoStream(rtsp_url).start()
    
    while True:
    frame = video_stream.read()
    if frame is None:
        continue
    
    frame = imutils.resize(frame,width=1200)
    
    frame = cv2.resize(frame, dsize=(1400, 600))
    
    
    cv2.imshow('Ip Camera ', frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
    
    cv2.destroyAllWindows()
    video_stream.stop()