首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV 'NoneType‘对象没有属性’NoneType‘

OpenCV 'NoneType‘对象没有属性’NoneType‘
EN

Stack Overflow用户
提问于 2022-11-06 19:14:30
回答 1查看 49关注 0票数 -2

我正在努力使其工作:https://circuitdigest.com/tutorial/real-life-object-detection-using-opencv-python-detecting-objects-in-live-video (滚动到“使用ORB的对象检测”部分)

`

代码语言:javascript
复制
import cv2
import numpy as np


def ORB_detector(new_image, image_template):
    # Function that compares input image to template
    # It then returns the number of ORB matches between them
    image1 = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)

    # Create ORB detector with 1000 keypoints with a scaling pyramid factor of 1.2
    orb = cv2.ORB_create(1000, 1.2)

    # Detect keypoints of original image
    (kp1, des1) = orb.detectAndCompute(image1, None)

    # Detect keypoints of rotated image
    (kp2, des2) = orb.detectAndCompute(image_template, None)

    # Create matcher 
    # Note we're no longer using Flannbased matching
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    # Do matching
    matches = bf.match(des1,des2)

    # Sort the matches based on distance.  Least distance
    # is better
    matches = sorted(matches, key=lambda val: val.distance)
    return len(matches)

cap = cv2.VideoCapture(0)

# Load our image template, this is our reference image
image_template = cv2.imread("phone.jpg", 0) 
# image_template = cv2.imread('images/kitkat.jpg', 0) 

while True:
    # Get webcam images
    ret, frame = cap.read()

    # Get height and width of webcam frame
    height, width = frame.shape[:2]

    # Define ROI Box Dimensions (Note some of these things should be outside the loop)
    top_left_x = int(width / 3)
    top_left_y = int((height / 2) + (height / 4))
    bottom_right_x = int((width / 3) * 2)
    bottom_right_y = int((height / 2) - (height / 4))

    # Draw rectangular window for our region of interest
    cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)

    # Crop window of observation we defined above
    cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]

    # Flip frame orientation horizontally
    frame = cv2.flip(frame,1)

    # Get number of ORB matches 
    matches = ORB_detector(cropped, image_template)

    # Display status string showing the current no. of matches 
    output_string = "Matches = " + str(matches)
    cv2.putText(frame, output_string, (50,450), cv2.FONT_HERSHEY_COMPLEX, 2, (250,0,150), 2)

    # Our threshold to indicate object deteciton
    # For new images or lightening conditions you may need to experiment a bit 
    # Note: The ORB detector to get the top 1000 matches, 350 is essentially a min 35% match
    threshold = 250

    # If matches exceed our threshold then object has been detected
    if matches > threshold:
        cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), (0,255,0), 3)
        cv2.putText(frame,'Object Found',(50,50), cv2.FONT_HERSHEY_COMPLEX, 2 ,(0,255,0), 2)

    cv2.imshow('Object Detector using ORB', frame)
    if cv2.waitKey(1) == 13: #13 is the Enter Key
        break

cap.release()
cv2.destroyAllWindows()

`

由于某些原因,我在‘高度,宽度= frame.shape:2’的第41行上得到了一个错误,我无法让它工作(错误是标题),我尝试了修复图像路径,但这没有起作用。我在木星实验室做这个。

我试着修复图像路径并检查它的.shape是否工作(所以是image_template.shape),这是许多其他堆栈溢出所告诉的。

EN

回答 1

Stack Overflow用户

发布于 2022-11-06 19:21:11

你的读物不会返回一个帧。以下代码:

代码语言:javascript
复制
cap = cv2.VideoCapture(0)
ret, frame = cap.read()

应该检查你是否真的成功地读取了一个框架。如果失败,cap.read()将返回False,因此您需要:

代码语言:javascript
复制
if not ret:
    print("Couldn't read a frame!")
    ... do something to handle this failure

因为读取失败,所以frameNone,所以当调用frame.shape时,python会抱怨'NoneType' object has no attribute 'shape'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74338968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档