Unable to Pass Video Frame for Object Detection Tensorflow python - opencv

I am able to process the video frames by saing the frame as an image and then processing it. But was unable to pass frame directly to the object detection.
Saving image with imwrite is making program slow...
Here is my main method:
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
# Window
while cv2.getWindowProperty("CSI Camera", 0) >= 0:
ret_val, frame = cap.read()
if not ret_val:
break
frame = imutils.resize(frame, width=600)
#cv2.imwrite('box.jpg', frame)
#image = Image.open(path)
#Error in here!!!
predictions = od_model.predict_image(frame)
for x in range(len(predictions)):
probab = (predictions[x]['probability'])*100
if(probab > 45):
print(predictions[x]['tagName'], end=' ')
print(probab)
#cv2.imshow("CSI Camera", frame)
# This also acts as
keyCode = cv2.waitKey(30) & 0xFF
# Stop the program on the ESC key
if keyCode == 27:
break
cap.release()
cv2.destroyAllWindows()
else:
print("Unable to open camera")
Error Message:
predictions = od_model.predict_image(frame)
File "/home/bharat/New_IT3/object_detection.py", line 125, in
predict_image
inputs = self.preprocess(image)
File "/home/bharat/New_IT3/object_detection.py", line 130, in
preprocess
image = image.convert("RGB") if image.mode != "RGB" else image
AttributeError: 'numpy.ndarray' object has no attribute 'mode'

open cv reads image in bgr colour spectrum conver it to rgb and send the image for detection, api for the same is -
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Related

Getting a (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

i am getting a (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' while making a face detection using openCv. Here is my code:
import cv2
import numpy as np
# Load HAAR face classifier
face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')
# Load functions
def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return None
# Crop all faces found
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
# Initialize Webcam
cap = cv2.VideoCapture(0)
count = 0
# Collect 100 samples of your face from webcam input
while True:
ret, frame = cap.read()
if face_extractor(frame) is not None:
count += 1
face = cv2.resize(face_extractor(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Save file in specified directory with unique name
file_name_path = './faces/user/' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)
# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Cropper', face)
else:
print("Face not found")
pass
if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()
print("Collecting Samples Complete")
and here is the output which is an error
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
I tried converting / to \ but that resulted in another SyntaxError: EOL while scanning string literal.
Please help me solving this issue i have also tr
You need to specify the path to your haarcascade_frontalface_default.xml file.
Put double \ in your path as i did
I modified your code and hope it helps
`#importing library
import cv2
import numpy as np
# Load HAAR face classifier
face_classifier =
cv2.CascadeClassifier
('D:\\OpenCV\\opencv\\build\\etc\\Haarcascades\\haarcascade_frontalface_default.xml')
# crop the image into 20 by 20 px and change it to B&W
def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return None
# Crop all faces found
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
#Asks for the total number of users
num=int(input("Enter no of user you want to add "))
user=1
# Initialize Webcam
cap = cv2.VideoCapture(0)
#count the total number of faces
count = 0
print("Start Capturing the input Data Set ")
d=input('Enter Face name')
# Collect 100 samples of your face from webcam input
while True:
ret, frame = cap.read()
print(type(frame))
#condition to check if face is in the frame or not
if face_extractor(frame) is not None:
count += 1
face = cv2.resize(face_extractor(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Save file in specified directory with unique name
file_name_path = './faces/user/' +str(d)+'-' +str(count) + '.jpg'
cv2.imwrite(file_name_path, face)
# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Cropper', face)
else:
print("Face not found")
pass
#checks if ech user 100 images is captured
if(count%100)==0 and count!= num*100 and count!=0:
print("Place the new user Signature")
cv2.waitKey()
#checks if total images are captured
if cv2.waitKey(1) == 13 or count == num*100: #13 is the Enter Key
break
#closes all windows
cap.release()
cv2.destroyAllWindows()
print("Collecting Samples Complete")`

Saving multiple videos at the same time with opencv

For my project I have multiple cameras and I need to save the video feed to multiple output files at the same time. Here's my code:
import cv2
def one_cam_is_open(list_of_cams):
for cam in list_of_cams:
if cam.isOpened():
return True
return False
cam_list = []
# Capture video from camera
for i in range(0, 2):
cap = cv2.VideoCapture(i)
if cap!=-1:
cam_list.append(cv2.VideoCapture(i))
# the width and height of all cameras will be 1900x1080
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
dim = (1900, 1080)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use the lower case
video_writers = {}
for i, cam in enumerate(cam_list):
video_writers[cam] = cv2.VideoWriter("output_" + str(i) + ".mp4", fourcc, 20.0, dim)
while one_cam_is_open(cam_list):
for (cam, out) in video_writers:#TypeError: cannot unpack non-iterable cv2.VideoCapture object
ret, frame = cam.read()
if ret == True:
frame = cv2.flip(frame, 0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame', frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break
else:
break
# Release everything if job is finished
for (cam, out) in video_writers:
out.release()
cam.release()
cv2.destroyAllWindows()
As noted in the code, I get the error TypeError: cannot unpack non-iterable cv2.VideoCapture object when I try to unpack the references to the VideoWriter obejects for each camera.
Is there a way to write to multiple video files using VideoWriter?

OpenCV3 does not properly release camera

System Architecture:
Fedora 27
OpenCV 3.4
Python 3.6
The issue produced from the following code:
def capture_input():
cap = cv2.VideoCapture(0)
while 1:
# Take each frame
ret = cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 45)
ret = cap.set(cv2.CAP_PROP_FRAME_WIDTH, 45)
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
cv2.imshow("Detecting Digits Frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
frame = cv2.resize(frame, (45, 45))
break
cap.release()
cv2.destroyAllWindows()
return frame
The code works as expected sometimes, the other times it refuses to run producing:
select timeout
cv2.error:/io/opencv/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat
Not only that. Moreover, the webcam does not even work after that, I tried opening it with Cheese, it show a black screen and buttons are grayed out.
I tried to follow these SO links:
Issue 1
Issue 2
but with no success, and same for the other GitHub links.
Thanks in advance.

Opencv can't write video: cap_mjpeg_encoder.cpp:829: error: (-215)

I use opencv to open a video, process frame by frame and write it back to a video. For some videos, this works fine, but for others, I got this wired error when I try to write the frames back to a video:
cv2.error: /home/xxx/Documents/opencv_videos/opencv/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write
Here is what my code looks like:
cap = cv2.VideoCapture('cut.avi')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc, 25.0, (1280,720))
count = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
processed_frame = PROCESS(frame)
cv2.imwrite('temp.jpg',processed_frame)
out.write(processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
I can, however, use cv2.imwrite to write the process frame to a jpeg image on disk. Does anyone know where this error comes from? Thanks!

OpenCV append frame to existing video (.avi) file?

i have a small question. Is it possible to add (append) new frames to an existing .avi video file using OpenCV, without overwriting the whole file ? I am using OpenCV2.4.2 on Windows7, with QT.
If you want to use OpenCV, you have to read and write all the data content.
import cv2
import os
# this two lines are for loading the videos.
# in this case the video are named as: cut1.mp4, cut2.mp4, ..., cut15.mp4
# videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.mp4']
# videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))
videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.avi']
videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))
video_index = 0
cap = cv2.VideoCapture(videofiles[0])
# video resolution: 1624x1234 px
# out = cv2.VideoWriter("video.avi",
# cv2.cv.CV_FOURCC('F','M','P', '4'),
# 15, (1624, 1234), 1)
# fourcc = cv2.VideoWriter_fourcc(*'MP4V')
# out = cv2.VideoWriter('cutout.mp4', fourcc, 20, (640, 480))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('cutout.avi', fourcc, 20.0, (640, 480))
while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
print ("end of video " + str(video_index) + " .. next one now")
video_index += 1
if video_index >= len(videofiles):
break
cap = cv2.VideoCapture(videofiles[ video_index ])
ret, frame = cap.read()
cv2.imshow('frame',frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
print ("end.")
Have a look at the VideoWriter class.

Resources