opencv how to use cascade on screen recorder - python

So I'm new to opencv and after practicing with some face detectors and understanding how to use the library, I created my own cascade and it's supposed to identify icons on my computer such as logos and others.
first to make sure my cascade worked I wrote one the detects the icons from the images I took,I took a screenshot and processed it through the cascade as an image and worked fine. the code for that is
import numpy as np
import cv2
img = cv2.imread('body.jpg')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_csc.detectMultiScale(gray, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('img',img)
cv2.waitKey(0)
after some time I wrote this for it to render my screen while detecting the icons the same way it did when I tried it on a screenshot:
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.waitKey(0)
out.release()
but when running the code it doesn't show any errors but it also doesn't detect or identify any of the icons it just records my screen, I've tried debugging this for hours now to no avail, any ideas?

You should show and write the video after you draw rectangles, not before.
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
cv2.waitKey(0)
out.release()

Related

I need some help it's about a face recognition python opencv module

I'm trying to do a face recognition that can detect faces and for some weird reason my code is giving me error
the error :
line 13, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-
m8us58q4\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in
function 'cv::cvtColor'
the code itself:
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while True:
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize = (20,20)
)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_color = img[y:y+h,x:x+w]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()
I would be very happy if some of the people who may read this will send me their solution by the way I'm very sorry about my indentation I'm just kinda new to stack overflow
It seems like your VideoCapture() did not grab frames.
Try to check the script on a saved video first (VideoCapture("vid_name.mp4")).
Try disabling the cv2.flip() and see if it helps, if yes, perhaps try move this flipping to after converting the source frame to gray.
Also it is a good idea to check the value of ret in ret, frame = cap.read() by using
if not ret:
print('no frame')
break
Then if the program exits with this error message, it means there's something wrong with the camera connection.
Modifying code like this worked for me. Putting the frame part outside the loop and then continuing:
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
if ret == False:
continue
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize = (20,20)
)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_color = img[y:y+h,x:x+w]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()
With regard to the error message you posted: The issue is the the HaarCascade classifer did not properly load due to the syntax in the load command. I was able to get it to work by
making sure I hade the file: https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
placing it into a known directory and modifying the load command to match.
...
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('c:\\Cascades\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
if ret == False:
continue
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize = (20,20))
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_color = img[y:y+h,x:x+w]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()

Eyes recognition using Haar-classifier of a static photo

it is possible to use haarclassifier on a static photo instead of the live streaming of the webcam?
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
I need to recognize just one or all eyes of the photo, without face.
Any suggest?
I edit from ur code
import cv2
#load haarcascade_eye
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# Load an color image
img = cv2.imread('Test_Image.jpg',cv2.IMREAD_COLOR)
#convert to gray scale to work with HAAR
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#read HAAR ade loop for find eye(s)
eyes = eye_cascade.detectMultiScale(gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
#show eye(s) rectangle in color image
cv2.imshow('img',img)
#press any key to close
cv2.waitKey(0)
cv2.destroyAllWindows()

Read all images from folder and detect faces, crop and save to new folder

I'm trying to build a model in which it will read all the images from a given folder and detects the face, crop and save cropped face to a new folder!
Can anyone help me with code as I am receiving an error:
cv2.imshow(str(img) , img)
TypeError: mat is not a numpy array, neither a scalar
code:
import glob
import cv2
import sys
while 1 :
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
#try :
var_img = cv2.imread(img)
cv2.imshow(str(img) , var_img)
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
cv2.imshow(str(img) , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
It looks like you're trying to imshow the filename instead of the actual array. glob.glob returns a list of filenames so your img that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img) so that means your array is var_img. But later you tried to imshow again using just img. You can only imshow var_img which is an array and not img which is a string.
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)

OpenCV 3.0 Beta - Python - Face Detection via Webcam - code gives me error

I wrote and looked over this code multiple times and it seems right to me especially that it's very simple and straight forward (Took snippet out of opencv tutorial), however it is still giving me an error. I've checked the error and I understand that it's missing channels, however, I don't know how to fix it. Any help would be appreciated. I'm using Windows x64 and Spyder (from Anaconda distribution) as my IDE. Could it be hardware issue? (webcam)
#####################################################
# Face Detection (Video) #
#####################################################
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video = cv2.VideoCapture(0)
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
Here's the error I get:
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:5731:
error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
i guess, you wanted a 'continuous detection, not a 'single shot' one, right ?
you're just some small changes away:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
if face_cascade.empty(): raise Exception("your face_cascade is empty. are you sure, the path is correct ?")
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
if eye_cascade.empty(): raise Exception("your eye_cascade is empty. are you sure, the path is correct ?")
video = cv2.VideoCapture(0)
while(video.isOpened()):
ret, frame = video.read()
if frame not None:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()

displaying the camera feed in grayscale in python with opencv

i've been trying to display the camera feed from my laptops web cam in grayscale and i've done it using the following code:
import cv2
import numpy as np
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.cv.CV_EVENT_LBUTTONUP:
clicked = True
cv2.namedWindow('image capture', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('image capture', onMouse)
#initialize the camera object with VideoCapture
camera = cv2.VideoCapture(0)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
while sucess and cv2.waitKey(1) == -1 and not clicked:
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image capture', gray)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
print 'photo taken press any key to exit'
cv2.waitKey()
cv2.destroyAllWindows()
Here what i've done is saved the frame in 'snapshot.png' and again reloaded it in grayscale and display that grayscale image. Is there any method to directly read the camera frame in grayscale rather than going through all this mess. Thanks in advance.
wow, what a mess ;)
you simply want:
gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
In the latest version of opencv, the cvtColor expects it's scr to be not None and therefore gives 215-assertion error.
This is basically like a scenario where you have to use a catch block and try to handle exceptions.
Code to overcome this problem:
while True:
ret, frame = cap.read()
if frame.any():
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)

Categories