简体   繁体   中英

AttributeError: 'tuple' object has no attribute 'shape' error when running opencv with Flask

I trying to stream object detection using Flask and when running my code I get this error. The code recognizes my webcam but i'm having trouble with this error

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

AttributeError: 'tuple' object has no attribute 'shape'

Here is the code I'm using:

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

check your frame with ret .

ret, frame = cap.read()

cap.read() will return 2 value. fist one a bool (True/False). If frame is read correctly, it will be True and False if your frame will not read correctly.

  • check frame.shape if your frame read correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM