将字节从 VideoCapture 帧转换为字符串

问题描述 投票:0回答:0

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 37: invalid start byte

def generate_frame():
    duration = 5
    global DetectName
    DetectedName='Unknown'
    cap=cv2.VideoCapture(0)
    while True:
        global counter
        if counter==0:
            counter=0
        counter+=1
        global DetectName
        DetectedName='Unknown'
        sucess, img = cap.read()
        if not sucess:
            break
        else:
            imgs = cv2.resize(img,(0,0),None, 0.25,0.25)
            imgs = cv2.cvtColor(imgs,cv2.COLOR_BGR2RGB)
            gray = cv2.cvtColor(imgs, cv2.COLOR_BGR2GRAY)
            img2=cv2.cvtColor(imgs,cv2.COLOR_RGB2BGR)
            #cv2.imshow('Webcam',img)
            facesCurFrame = fr.face_locations(imgs)
            encodeCurFrame = fr.face_encodings(imgs,facesCurFrame)

            for encodFace,faceloc in zip(encodeCurFrame,facesCurFrame):
                matches  =  fr.compare_faces(encodeKnown,encodFace)
                facedist =  fr.face_distance(encodeKnown,encodFace)
                matchIndex=numpy.argmin(facedist)
                if matches[matchIndex]:
                    global name
                    name = classNames[matchIndex]
                    DetectName=name
                    y1,x2,y2,x1 = faceloc
                    y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
                    cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
                    cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
            
            ret,buffer=cv2.imencode('.jpg',img)
            frame=buffer.tobytes()

            name12=str.encode(DetectName)
            yield(b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n'+frame+b'\r\n'+name12+b'\r\n')

获取错误:return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:“utf-8”编解码器无法解码位置 37 中的字节 0xff:起始字节无效 试过

@app.route('/video') 
def video():     
    a=generate_frame()     
    l=[]
    s=codecs.decode(next(a))
    l.append(s.split('\r\n'))     
    print(l)     #print(type(next(a)))     
    return Response(generate_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')
python python-3.x opencv utf-8 decode
© www.soinside.com 2019 - 2024. All rights reserved.