AttributeError:“tuple”对象在使用Flask运行opencv时没有属性“shape”错误

2024-04-24 23:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Flask流式对象检测,当运行我的代码时,我得到了这个错误。代码识别我的网络摄像头,但我遇到了此错误

File "C:\Users\Nicholas Smith\Downloads\Senior Project\Senior Project\app1.py", line 60, in gen
(h, w) = frame.shape

AttributeError:“tuple”对象没有属性“shape”

以下是我正在使用的代码:

print("[INFO] starting video stream...")
cap = cv2.VideoCapture(1)


if (cap.isOpened()== False):
    print("Error opening video stream or file")

while cap.isOpened():
    print("Its working")
    # grab the frame from the threaded video stream and resize it
    # # to have a maximum with of 400 pixels
    frame = cap.read()
    #frame1 = imutils.resize(frame, width=400)
    
    # grab the frame dimensions and convert it to a blob
    (h, w) = frame.shape
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
        0.007843, (300, 300), 127.5)
        
    # pass the blob through the network and obtain the detections and
    # # predictions
    net.setInput(blob)
    detections = net.forward()
    
    # loop over the detections
    for i in np.arange(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with
        # # the prediction
        confidence = detections[0, 0, i, 2]
        
        # filter out weak detections by ensuring the `confidence` is
        # # greater than the minimum confidence
        if confidence > 0.5:
            # extract the index of the class label from the
            # # `detections`, then compute the (x, y)-coordinates of
            # # the bounding box for the object
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            
            # draw the prediction on the frame
            label = "{}: {:.2f}%".format(CLASSES[idx],
                confidence * 100)
            cv2.rectangle(frame, (startX, startY), (endX, endY),
                COLORS[idx], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    
    cv2.imshow('Frame',frame)  
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Tags: andthe代码ifvideocv2frameblob
1条回答
网友
1楼 · 发布于 2024-04-24 23:01:26

ret检查帧

ret, frame = cap.read()

cap.read()将返回2个值。先打一个球(对/错)。如果帧读取正确,则将是True,如果帧读取不正确,则为False

  • 检查frame.shape是否正确读取了框架

相关问题 更多 >