嗨,
我正在使用Jetson Nano和Raspberry Pi相机模块V2,并且希望运行有关YOLO +对象检测的课程中的代码。
当我尝试使用Spyder(python 3.6)运行代码时,会发生此错误:
文件“ /home/dlinano/Documents/Python/UdemyTrainingYOLO/activity-1/detecting-object.py”,第44行, frame_HSV = cv2.cvtColor(frame_BGR,cv2.COLOR_BGR2HSV)
错误:OpenCV(4.1.1)/home/nvidia/host/build_opencv/nv_opencv/modules/imgproc/src/color.cpp:182:错误:(-215:断言失败)!_src.empty ()在函数“ cvtColor”中
这是代码:
# Detecting Object with chosen Colour Mask
#
# Algorithm:
# Reading RGB image --> Converting to HSV --> Implementing Mask -->
# --> Finding Contour Points --> Extracting Rectangle Coordinates -->
# --> Drawing Bounding Box --> Putting Label
#
# Result:
# Window with Detected Object, Bounding Box and Label in Real Time
# Importing needed library
import cv2
# Defining lower bounds and upper bounds of founded Mask
min_blue, min_green, min_red = 21, 222, 70
max_blue, max_green, max_red = 176, 255, 255
# Getting version of OpenCV that is currently used
# Converting string into the list by dot as separator
# and getting first number
v = cv2.__version__.split('.')[0]
# Defining object for reading video from camera
camera = cv2.VideoCapture(0)
# Defining loop for catching frames
while True:
# Capture frame-by-frame from camera
_, frame_BGR = camera.read()
# Converting current frame to HSV
frame_HSV = cv2.cvtColor(frame_BGR, cv2.COLOR_BGR2HSV)
# Implementing Mask with founded colours from Track Bars to HSV Image
mask = cv2.inRange(frame_HSV,
(min_blue, min_green, min_red),
(max_blue, max_green, max_red))
# Showing current frame with implemented Mask
# Giving name to the window with Mask
# And specifying that window is resizable
cv2.namedWindow('Binary frame with Mask', cv2.WINDOW_NORMAL)
cv2.imshow('Binary frame with Mask', mask)
# Finding Contours
# Pay attention!
# Different versions of OpenCV returns different number of parameters
# when using function cv2.findContours()
# In OpenCV version 3 function cv2.findContours() returns three parameters:
# modified image, found Contours and hierarchy
# All found Contours from current frame are stored in the list
# Each individual Contour is a Numpy array of(x, y) coordinates
# of the boundary points of the Object
# We are interested only in Contours
# Checking if OpenCV version 3 is used
if v == '3':
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# In OpenCV version 4 function cv2.findContours() returns two parameters:
# found Contours and hierarchy
# All found Contours from current frame are stored in the list
# Each individual Contour is a Numpy array of(x, y) coordinates
# of the boundary points of the Object
# We are interested only in Contours
# Checking if OpenCV version 4 is used
else:
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# Finding the biggest Contour by sorting from biggest to smallest
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# Extracting Coordinates of the biggest Contour if any was found
if contours:
# Getting rectangle coordinates and spatial size from biggest Contour
# Function cv2.boundingRect() is used to get an approximate rectangle
# around the region of interest in the binary image after Contour was found
(x_min, y_min, box_width, box_height) = cv2.boundingRect(contours[0])
# Drawing Bounding Box on the current BGR frame
cv2.rectangle(frame_BGR, (x_min - 15, y_min - 15),
(x_min + box_width + 15, y_min + box_height + 15),
(0, 0, 255), 3)
# Preparing text for the Label
label = 'Detected Object'
# Putting text with Label on the current BGR frame
cv2.putText(frame_BGR, label, (x_min - 5, y_min - 25),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
# Showing current BGR frame with Detected Object
# Giving name to the window with Detected Object
# And specifying that window is resizable
cv2.namedWindow('Detected Object', cv2.WINDOW_NORMAL)
cv2.imshow('Detected Object', frame_BGR)
# Breaking the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Destroying all opened windows
cv2.destroyAllWindows()
我已经检查了以下内容:
1。) dlinano @ jetson-nano:〜$ ls -l / dev / video0 crw-rw ---- + 1个根视频81,3月21日0:56 / dev / video0 (我认为这很好)
2。) gst-launch-1.0 nvarguscamerasrc sensor_mode = 0! ‘video / x-raw(内存:NVMM),宽度= 3820,高度= 2464,帧速率= 21/1,格式= NV12”! nvvidconv flip-method = 0! “ video / x-raw,width = 960,height = 616”! nvvidconv! nvegltransform! nveglglessink -e (一切正常,图像质量也不错)
3。) 在装有Spyder(python 3.6)的Windows 10笔记本和车载摄像头的笔记本上运行相同的代码:结果:一切正常
有人可以帮助我吗?
BR克里斯