由于 Flask 集成的帧到字节的 OpenCV 问题

我正在使用以下代码:


def gen_frames():  # generate frame by frame from camera

    while True:

        # Capture frame-by-frame

        success, frame = camera.read()  # read the camera frame

        if not success:

            break

        else:

            ret, buffer = cv2.imencode('.jpg', frame)

            frame = buffer.tobytes()

            yield (b'--frame\r\n'

                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')  # concat frame one by one and show result

            #frame_resized = cv2.resize(frame,None,fx=0.75,fy=0.75)

            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

我收到一个错误:


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

TypeError: Expected Ptr<cv::UMat> for argument 'src'

这是正常的,因为帧数组被转换为字节。但这对于 Flask 中的视频输入来说是必需的:


def video_feed():

    """Video streaming route. Put this in the src attribute of an img tag."""

    return Response(gen_frames(),

    mimetype='multipart/x-mixed-replace; boundary=frame')

我应该如何使其发挥作用?


Qyouu
浏览 121回答 1
1回答

暮色呼如

已修复,需要移动一些代码。现在的解决方案是:from flask import Flask, render_template, Responseimport numpy as npimport cv2import pickleimport rtspimport PIL as Imageimport threadingimport timeimport queueapp = Flask(__name__)url = 'rtsp://user:password@192.168.1.xxx:YYYY/stream0/mobotix.mjpeg'#camera = cv2.VideoCapture(url)&nbsp; # use 0 for web camera#&nbsp; for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of cameraface_cascade = cv2.CascadeClassifier('cascades\data\haarcascade_frontalface_alt2.xml')recognizer = cv2.face.LBPHFaceRecognizer_create()recognizer.read("trainner.yml")labels = {"person_name": 1}with open("labels.pickle", 'rb') as f:&nbsp; &nbsp; og_labels = pickle.load(f)&nbsp; &nbsp; labels = {v:k for k,v in og_labels.items()}class VideoCapture:&nbsp; def __init__(self, name):&nbsp; &nbsp; self.cap = cv2.VideoCapture(name)&nbsp; &nbsp; self.q = queue.Queue()&nbsp; &nbsp; t = threading.Thread(target=self._reader)&nbsp; &nbsp; t.daemon = True&nbsp; &nbsp; t.start()&nbsp; # read frames as soon as they are available, keeping only most recent one&nbsp; def _reader(self):&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; ret, frame = self.cap.read()&nbsp; &nbsp; &nbsp; if not ret:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; if not self.q.empty():&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.q.get_nowait()&nbsp; &nbsp;# discard previous (unprocessed) frame&nbsp; &nbsp; &nbsp; &nbsp; except queue.Empty:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; self.q.put(frame)&nbsp; def read(self):&nbsp; &nbsp; return self.q.get()def gen_frames():&nbsp; &nbsp; cap = VideoCapture(url)&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(.5)&nbsp; &nbsp;# simulate time between events&nbsp; &nbsp; &nbsp; &nbsp; frame = cap.read()&nbsp; &nbsp; &nbsp; &nbsp; frame_detection(frame)&nbsp; &nbsp; &nbsp; &nbsp; ret, buffer = cv2.imencode('.jpg', frame)&nbsp; &nbsp; &nbsp; &nbsp; frame = buffer.tobytes()&nbsp; &nbsp; &nbsp; &nbsp; yield (b'--frame\r\n'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')&nbsp; # concat frame one by one and show resultdef frame_detection(frame):&nbsp; &nbsp; gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)&nbsp; &nbsp; faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)&nbsp; &nbsp; for (x,y,w,h) in faces:&nbsp; &nbsp; &nbsp; print(x,y,w,h)&nbsp; &nbsp; &nbsp; roi_gray = gray[y:y+h, x:x+w]&nbsp; &nbsp; &nbsp; roi_color = frame[y:y+h, x:x+w]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; #recognize?&nbsp; &nbsp; &nbsp; id_, conf = recognizer.predict(roi_gray)&nbsp; &nbsp; &nbsp; if conf>=45: # and conf <=85:&nbsp; &nbsp; &nbsp; &nbsp; print(id_)&nbsp; &nbsp; &nbsp; &nbsp; print(labels[id_])&nbsp; &nbsp; &nbsp; &nbsp; font = cv2.FONT_HERSHEY_SIMPLEX&nbsp; &nbsp; &nbsp; &nbsp; name = labels[id_]&nbsp; &nbsp; &nbsp; &nbsp; color = (0,0,255)&nbsp; &nbsp; &nbsp; &nbsp; stroke = 2&nbsp; &nbsp; &nbsp; &nbsp; cv2.putText(frame, name, (x,y), font, 1, color, stroke, cv2.LINE_AA)&nbsp; &nbsp; &nbsp; &nbsp; #img_item = "my-image.png"&nbsp; &nbsp; &nbsp; &nbsp; #cv2.imwrite(img_item, roi_gray)&nbsp; &nbsp; &nbsp; &nbsp; color = (0, 0, 255)&nbsp; &nbsp; &nbsp; &nbsp; stroke = 2&nbsp; &nbsp; &nbsp; &nbsp; end_cord_x = x + w&nbsp; &nbsp; &nbsp; &nbsp; end_cord_y = y + h&nbsp; &nbsp; &nbsp; &nbsp; cv2.rectangle(frame, (x,y), (end_cord_x, end_cord_y), color, stroke)&nbsp; &nbsp; &nbsp; &nbsp; return frame@app.route('/video_feed')def video_feed():&nbsp; &nbsp; """Video streaming route. Put this in the src attribute of an img tag."""&nbsp; &nbsp; return Response(gen_frames(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mimetype='multipart/x-mixed-replace; boundary=frame')@app.route('/')def index():&nbsp; &nbsp; """Video streaming home page."""&nbsp; &nbsp; return render_template('index.html')if __name__ == '__main__':&nbsp; &nbsp; app.run(host='0.0.0.0')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python