Errors when attempting to run object detection - python

The code that im trying to run
import cv2
cap = cv2.VideoCapture(1)
cap.set(3,640)
cap.set(4,480)
classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img = cap.read()
classIds, confs, bbox = net.detect(img, confThreshold= 0.5)
print(classIds, bbox)
for classId, confidence, box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness= 3)
cv2.putText(img,classNames[classId-1].upper(),(box[0]+10, box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(100,255,50),2)
cv2.imshow("Output", img)
cv2.waitKey(1)
after running the code, there are errors that occurred such as;
C:\Users\david\PycharmProjects\ObjectDetectionTest2\venv\Scripts\python.exe C:/Users/david/PycharmProjects/ObjectDetectionTest2/main.py
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
File "C:\Users\david\PycharmProjects\ObjectDetectionTest2\main.py", line 23, in <module>
classIds, confs, bbox = net.detect(img, confThreshold= 0.5)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
Many people said that this occurred because the image directory isn't the same or the image contains zero pixels. I have tried to move the coco, pbtxt, pb files to the same directory as the main.py file but it still did nothing.
However, since this is my first time coding and I followed a tutorial on youtube, I have very little knowledge about coding and I hope you can help in human language lol.
Any help and insights would be appreciated!

I had the same error
cap = cv2.VideoCapture(0)
If you are not using a webcam, it should be VideoCapture(0).

Related

error OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imwrite'

so im try to use opencv to make extrack picture from video and using this code:
import cv2
vidcap = cv2.VideoCapture("D:\danil file\KULIAH\test.mp4")
count = 0
while vidcap.isOpened:
image = vidcap.read()
cv2.imwrite("D:\danil file\KULIAH\%d.png" % count, image )
count+=1
and somehow it error and says" OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imwrite'
Overload resolution failed:
img is not a numerical tuple
Expected Ptr<cv::UMat> for argument 'img'"
can u guys help me to make the program run?
vidcap.read() return two variables , change your code as below to fix your issue
import cv2
vidcap = cv2.VideoCapture("D:\danil file\KULIAH\test.mp4")
count = 0
while vidcap.isOpened():
ret, image = vidcap.read()
if ret == True:
cv2.imwrite("D:\danil file\KULIAH\%d.png" % count, image )
count+=1
else:
break

Issue's with running my trainer.py file using opencv

After getting my facial capture component to work (I can gather photos that will be used for the dataset) I am trying to run a trainer.py file that will train the module but I am having some issues with my code. I am new to all of this so someone who is experienced with OpenCV, face rec and python might be able to help me a bit more.
Here is my code.
https://pastebin.com/uSX0Z8qa
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = "dataset"
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces, ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Prints the number of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
And here is the error I am getting after running it
[INFO] Training faces. It will take a few seconds. Wait ... Traceback (most recent call last): File "c:\Users\Ben\Desktop\Code\OpenCV\trainer\trainer.py", line 34, in <module> faces, ids = getImagesAndLabels(path) File "c:\Users\Ben\Desktop\Code\OpenCV\trainer\trainer.py", line 25, in getImagesAndLabels faces = detector.detectMultiScale(img_numpy) tion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Stream video with open cv in python

Python version = 3.10.2
open cv version = 4.5.5.62
code:
import cv2
a = cv2.VideoCapture('C:\21-240p.mp4')
while(True):
ret, frame = a.read()
cv2.imshow('show',frame)
#cv2.waitKey(1) == ord('q')
#cv2.destroyWindow('show')
errors:
Traceback (most recent call last):
File "C:\Users\asd\imreadwrite2.py", line 5, in
cv2.imshow('show',frame)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-
python\opencv\modules\imgproc\src\color.cpp:182: error:
(-215:Assertion failed) !_src.empty() in
function 'cv::cvtColor'
I think it's an escape character issue. If you'll print your file path you'll get C:-240p.mp4 so just add r before the path (avoids escape characters in strings) and you'll get C:\21-240p.mp4
import cv2
import os
vid_path_bad = 'C:\21-240p.mp4'
print(vid_path_bad)
vid_path = r'C:\21-240p.mp4' # notice the r
print(vid_path)
if not os.path.exists(vid_path): # good to always check we can find the file
print('file {} not found'.format(vid_path))
exit(-1)
a = cv2.VideoCapture(vid_path)
while (True):
success, frame = a.read()
if success: # frame read successfully
cv2.imshow('show', frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
cv2.destroyAllWindows()

How to solve the "NameError: name 'frame' is not defined" error occured in OpenCV?

import imgaug.augmenters as iaa
import cv2
import glob
from tkinter import Frame
from tkinter import Text
from tkinter import Label
# 1. Load Dataset
images = []
images_path = glob.glob("images/*.jpg")
for img_path in images_path:
img = cv2.imread(img_path)
images.append(img)
# 2. Image Augmentation
augmentation = iaa.Sequential([
# 1. Flip
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
# 2. Affine
iaa.Affine(translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
rotate=(-30, 30),
scale=(0.5, 1.5)),
# 3. Multiply
iaa.Multiply((0.8, 1.2)),
# 4. Linearcontrast
iaa.LinearContrast((0.6, 1.4)),
# Perform methods below only sometimes
iaa.Sometimes(0.5,
# 5. GaussianBlur
iaa.GaussianBlur((0.0, 3.0))
)
])
# 3. Show Images
counter = 0
while True:
augmented_images = augmentation(images=images)
for img in augmented_images:
counter += 1
cv2.imwrite(str(counter) + ".jpg", frame)
cv2.imwrite('Desktop/images/dog.jpg', img) # desired save location
cv2.waitKey(0)
"Traceback (most recent call last):
File "C:/Users/MC/PycharmProjects/pythonProject/Python_Augmentation.py", line 48, in
cv2.imwrite(str(counter) + ".jpg", frame)
NameError: name 'frame' is not defined"
The image augmentation code ran well, but when I tried to save the augmented images this error occurred. I also used capital 'F' instead of 'f' (frame), but I got another error.
"Traceback (most recent call last):
File "C:/Users/MC/PycharmProjects/pythonProject/Python_Augmentation.py", line 49, in
cv2.imwrite(str(counter) + ".jpg", Frame)
cv2.error: OpenCV(4.5.4-dev) :-1: error: (-5:Bad argument) in function 'imwrite'
Overload resolution failed:
img is not a numpy array, neither a scalar
Expected Ptr<cv::UMat> for argument 'img'"
Any type of help is appreciatable. Thanks in advance.
The import was:
from tkinter import Frame
but you using, in line 48: frame (lower 'f').

OpenCV fails on resizing

I'm using Python with OpenCV, and I'm trying to open the camera. However, I'm getting the following error:
VIDEOIO ERROR: V4L: can't open camera by index 1
VIDEOIO ERROR: V4L: can't open camera by index 0
Traceback (most recent call last):
File "set_hand_hist.py", line 71, in <module>
get_hand_hist()
File "set_hand_hist.py", line 38, in get_hand_hist
img = cv2.resize(img, (640, 480))
cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
I believe that this is the corresponding part of the Python code:
def store_images(g_id):
total_pics = 1200
hist = get_hand_hist()
cam = cv2.VideoCapture(1)
if cam.read()[0]==False:
cam = cv2.VideoCapture(0)
x, y, w, h = 300, 100, 300, 300
create_folder("gestures/"+str(g_id))
pic_no = 0
flag_start_capturing = False
frames = 0
.... ... remaining code ... ...
I've tried looking on Google for a solution; however, nothing that I found yet works. I would really appreciate it if someone who is more experienced could take a look and try to help me out.
Thanks so much
Wanted to comment but I lack reputation. Seems like you can't open both cameras 1 and 0.
VIDEOIO ERROR: V4L: can't open camera by index 1
VIDEOIO ERROR: V4L: can't open camera by index 0
Please be sure a camera is connected to the system. If so, check that you have the appropriate Linux drivers installed for your camera.
And on line 38 of set_hand_hist.py, img variable is possibly an empty image. That's why you get an error on resize. You should investigate why img image is empty. Can't tell more without seeing the whole code.
Edit:You can try the code below to read images from the camera and show it. I believe the error is reading images from the camera. If the code below works, we should look elsewhere for the error.
import cv2
camera_index=1
cam = cv2.VideoCapture(1)
if not cam.read()[0]:
cam = cv2.VideoCapture(0)
camera_index=0
while True:
ret, frame = cam.read()
cv2.imshow(f"image from camera {camera_index}", frame)
if not ret:
break
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
cam.release()
cv2.destroyAllWindows()

Categories

Resources