pythonmacosopencvmjpeg

Cropping video with OpenCV in Python (Mac)


As a whole I am hoping to access ip camera using OpenCV, crop and adjust their image properties (saturation, contrast, brightness) and then output the result as a new stream.

I have very little knowledge of python/opencv and am doing my best to piece this together from what I can find.

I have been able to access the mjpeg stream however every way of cropping I have found seems to fail. The code below seems the most promising but I am open to alternative methods.

I have achieved the result i'm after using Max MSP and Syphon however my hope is that using OpenCV i will be able to make this completely web based and accessible.

I am hoping to avoid splitting the stream into individual jpegs but if that is the only way to achieve what i'm after please let me know.

Any and all guidance is greatly appreciated.


import cv2
import numpy as np

cap = cv2.VideoCapture('http://89.29.108.38:80/mjpg/video.mjpg')

(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 20)
roi = frame[y:y+h, x:x+w]

while True:
  ret, frame = cap.read()
  cv2.imshow('Video', frame)

  if cv2.waitKey(1) == 27:
    exit(0)

Traceback (most recent call last):
  File "mjpeg-crop.py", line 6, in <module>
    (x, y, w, h) = cv2.boundingRect(c)
NameError: name 'c' is not defined

Solution

  • Too much for a comment, but try this to get started:

    import cv2
    import numpy as np
    
    cap = cv2.VideoCapture('http://89.29.108.38:80/mjpg/video.mjpg')
    
    # (x, y, w, h) = cv2.boundingRect(c)
    # cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 20)
    # roi = frame[y:y+h, x:x+w]
    
    while True:
        ret, frame = cap.read()
        # (height, width) = frame.shape[:2]
        sky = frame[0:100, 0:200]
        cv2.imshow('Video', sky)
    
        if cv2.waitKey(1) == 27:
            exit(0)