I am trying to detect a simple object, and then detect his color. I have created my own CUSTOM HAARCASCADE but it converts the camera's image to grayscale. Maybe I could detect the object through a mask? I have not found any articles on this online.
Here is my code if u need it:
import cv2
import numpy as np
################################################################
#path = 'haarcascades/haarcascade_eye.xml' # PATH OF THE CASCADE
path = 'haarcascades/Azuolas.xml' # PATH OF THE CASCADE
#path = 'haarcascades/haarcascade_frontalface_default.xml' # PATH OF THE CASCADE
#path = 'haarcascades/haarcascade_smile.xml' # PATH OF THE CASCAD
objectName = 'Azuolas' # OBJECT NAME TO DISPLAY
frameWidth= 640 # DISPLAY WIDTH
frameHeight = 480 # DISPLAY HEIGHT
color= (255,0,255)
#################################################################
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
def empty(a):
pass
# CREATE TRACKBAR
cv2.namedWindow("Result")
cv2.resizeWindow("Result",frameWidth,frameHeight+100)
cv2.createTrackbar("Scale","Result",400,1000,empty)
cv2.createTrackbar("Neig","Result",8,50,empty)
cv2.createTrackbar("Min Area","Result",0,100000,empty)
cv2.createTrackbar("Brightness","Result",180,255,empty)
# LOAD THE CLASSIFIERS DOWNLOADED
cascade = cv2.CascadeClassifier(path)
while True:
# SET CAMERA BRIGHTNESS FROM TRACKBAR VALUE
cameraBrightness = cv2.getTrackbarPos("Brightness", "Result")
cap.set(10, cameraBrightness)
# GET CAMERA IMAGE AND CONVERT TO GRAYSCALE
success, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# DETECT THE OBJECT USING THE CASCADE
scaleVal =1 + (cv2.getTrackbarPos("Scale", "Result") /1000)
neig=cv2.getTrackbarPos("Neig", "Result")
objects = cascade.detectMultiScale(gray,scaleVal, neig)
# DISPLAY THE DETECTED OBJECTS
for (x,y,w,h) in objects:
print(objectName ,"is in my fov")
area = w*h
minArea = cv2.getTrackbarPos("Min Area", "Result")
if area >minArea:
cv2.rectangle(img,(x,y),(x+w,y+h),color,3)
cv2.putText(img,objectName,(x,y-5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,color,2)
roi_color = img[y:y+h, x:x+w]
cv2.imshow("Result", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Thanks in advance!!!!
I'm familiar with cv2, but haven't used it in years, so bear with me here. What I think you're saying is that when you call cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) it's overwriting the original img to grayscale, even though you've assigned it to the variable gray.
You probably need to copy the image before converting to grayscale. Clone an image in cv2 python
gray = cv2.cvtColor( img.copy(), cv2.COLOR_BGR2GRAY )
Related
i need to track my cursor to the nose of a human to create a DIY aimbot with pose detection.
(just for fun, not intending to cheat, there would be so many better and easier options than to make my own)
i already have the first part of the code and it shows you your screen and the skeleton, as well as the exact coordinates of the nose with no problem,
but the method that im using to move my cursor over to that point is not working
im using mouse.move and have tried other stuff like pyautogui, tkinter.
it doesn't give me an error but still does not work
import cv2
import mediapipe as mp
import numpy as np
import time
import pyautogui
import mouse
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# display screen resolution, get it from your OS settings
SCREEN_SIZEX = (1920)
SCREEN_SIZEY = (1080)
# define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZEX, SCREEN_SIZEY))
with mp_pose.Pose(min_detection_confidence=0.1, min_tracking_confidence=0.9) as pose:
while True:
# make a screenshot
img = pyautogui.screenshot()
# convert these pixels to a proper numpy array to work with OpenCV
frame = np.array(img)
# convert colors from BGR to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Recolor image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make detection
results = pose.process(image)
# Recolor back to BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
try:
landmarks = results.pose_landmarks.landmark
lndmark = landmarks[mp_pose.PoseLandmark.NOSE.value]
x = [landmarks[mp_pose.PoseLandmark.NOSE.value].x]
y = [landmarks[mp_pose.PoseLandmark.NOSE.value].y]
#print(x)
#print(y)
mouse.move(x, y)
except:
pass
# Render detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
)
# write the frame
out.write(frame)
pTime = 0
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(image, str(int(fps)), (20, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 0), 3)
cv2.imshow('Mediapipe Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
out.release()
cv2.destroyAllWindows()
#for lndmark in mp_pose.PoseLandmark:
#print(lndmark)
this is the part that doesn't work:
try:
landmarks = results.pose_landmarks.landmark
lndmark = landmarks[mp_pose.PoseLandmark.NOSE.value]
x = [landmarks[mp_pose.PoseLandmark.NOSE.value].x]
y = [landmarks[mp_pose.PoseLandmark.NOSE.value].y]
mouse.move(x, y)
except:
pass
i would assume that it is beacuse x and y are supposed to numbers or somehow it can't read or proccess it correctly
but it doesn't give me an error, so im asking it here hoping on of you guys had already figured this one out
Im using opencv to detect faces in my webcam and whenever a face is detected I want to crop the ROI of face and save its image locally on my system. But when I run the code it doesnt do anything at all.
My webcam is working in opencv(which Ive already tested). But this specific code isnt working, for unknown reasons.
import os
import cv2
ctr=0
# import face detection cascade
face_cascade = cv2.CascadeClassifier('/home/opi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml')
# create capture object
cap = cv2.VideoCapture(0)
while(True):
# capture frame-by-frame
ret, img = cap.read()
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(img, 1.3, 5)
# for each face draw a rectangle around and copy the face
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]
roi_gray = gray[y:y+h, x:x+w]
#SAVE THE DETECTED FACES LOCALLY
roi_gray=cv2.resize(roi_gray,(100,100))
cv2.imwrite('faces'+'/'+str(ctr)+'.jpg',roi_gray)
ctr+=1
# display the resulting frame
cv2.imshow('frame',img)
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
When I kill the code pressing Ctrl+C, I can see this error which also doesnt make much sense :-
opi#admin:~$ python input.py
^CTraceback (most recent call last):
File "input.py", line 18, in <module>
faces = face_cascade.detectMultiScale(img, 1.3, 5)
KeyboardInterrupt
You have forgotten to add cv2.waitKey(1) and that is why your code crashes while it is in an infinite loop ( while True: ... ). We can also say that there is no time for showing(refreshing) frames ...
Add this block of code:
# display the resulting frame
cv2.imshow('frame',img)
key = cv2.waitKey(1)
if key == 32: # if the space key is pressed
break
The final code :
import os
import cv2
ctr=0
# import face detection cascade
face_cascade = cv2.CascadeClassifier('/home/opi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_default.xml')
# create capture object
cap = cv2.VideoCapture(0)
while True:
# capture frame-by-frame
ret, img = cap.read()
# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(img, 1.3, 5)
# for each face draw a rectangle around and copy the face
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]
roi_gray = gray[y:y+h, x:x+w]
#SAVE THE DETECTED FACES LOCALLY
roi_gray=cv2.resize(roi_gray,(100,100))
cv2.imwrite('faces'+'/'+str(ctr)+'.jpg',roi_gray)
ctr+=1
# display the resulting frame
cv2.imshow('frame',img)
key = cv2.waitKey(1)
if key == 32: # if space key is pressed
break
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I am trying to crop a face everytime it appears in video but it does not seem to be creating the image file.
I have included an imshow line to see if there is an image being created from the bounding boxes and there is a face that gets cropped. Any help would be appreciated.
Code:
import numpy as np
import cv2
import os
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture("smallvid.mp4")
while(True):
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
count = 1
for (x,y,w,h) in faces:
cropped = img[ y : y+h, x : x+w ]
cv2.imwrite("cropped_face" + str(id) + ".png", cropped)
count=count+1
cv2.imshow(out_dir+str(count), cropped); # show an image of each face
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # create bounding box around face
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
enter code here
You can crop just like this.
img[y:y+w, x:x+w]
I am using opencv to detect the color of objects by using HSV trackbars values and I want my python code to save the latest changes I would make to the trackbars in opencv , when I start the code again, the trackbars will have the last values? below is my code
import numpy as np
import cv2
# open the camera
cap = cv2.VideoCapture(0)
def nothing(x):
pass
cv2.namedWindow('result')
# Starting with 100's to prevent error while masking
h,s,v = 100,100,100
# Creating track bar
cv2.createTrackbar('h', 'result',0,179,nothing)
cv2.createTrackbar('s', 'result',0,255,nothing)
cv2.createTrackbar('v', 'result',0,255,nothing)
while True:
#read the image from the camera
ret, frame = cap.read()
#You will need this later
frame = cv2.cvtColor(frame, 35)
#converting to HSV
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
# get info from track bar and appy to result
h = cv2.getTrackbarPos('h','result')
s = cv2.getTrackbarPos('s','result')
v = cv2.getTrackbarPos('v','result')
# Normal masking algorithm
lower_blue = np.array([h,s,v])
upper_blue = np.array([180,255,255])
mask = cv2.inRange(hsv,lower_blue, upper_blue)
result = cv2.bitwise_and(frame,frame,mask = mask)
cv2.imshow('result',result)
#find center
cnts=cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
center=None
if len(cnts)>0:
c=max(cnts, key=cv2.contourArea)
((x,y),radius)=cv2.minEnclosingCircle(c)
M=cv2.moments(c)
center=(int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius>10:
#cv2.circle(frame, (int(x),int(y)), int(radius), 2)
cv2.circle(frame, center,5,(0,0,255),-1)
# color detection limits
lB = 5
lG = 50
lR = 50
hB = 15
hG = 255
hR = 255
lowerLimits = np.array([lB, lG, lR])
upperLimits = np.array([hB, hG, hR])
# Our operations on the frame come here
thresholded = cv2.inRange(frame, lowerLimits, upperLimits)
outimage = cv2.bitwise_and(frame, frame, mask = thresholded)
cv2.imshow('original', frame)
# Display the resulting frame
cv2.imshow('processed',outimage)
# Quit the program when Q is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
print 'closing program'
cap.release()
cv2.destroyAllWindows()
One option is to write the values to a text file somewhere, then when the program starts, read the file and parse the values written in the file.
See: How could I save data after closing my program?
i'm working on face recognition project with my college. what i'm trying to take a snapshot and save it if the face is detected automatically before closing the webcam.
what I have now is open cam and wait if face is detected and press "q" to take snapshot and save the image.
Here is the code:
import numpy as np
import cv2
import time
#import the cascade for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def TakeSnapshotAndSave():
# access the webcam (every webcam has a number, the default is 0)
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# to detect faces in video
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]
x = 0
y = 20
text_color = (0,255,0)
# write on the live stream video
cv2.putText(frame, "Press q when ready", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=2)
# if you want to convert it to gray uncomment and display gray not fame
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',frame)
# press the letter "q" to save the picture
if cv2.waitKey(1) & 0xFF == ord('q'):
# write the captured image with this name
cv2.imwrite('try.jpg',frame)
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
TakeSnapshotAndSave()
thank you in advance
I adapted your code to save 10 images just for testing, if you want infinite photos, just change the while condition. So in your code you were overwriting the current image so I changed the string parameter so that it was possible to take lots of pictures.
import numpy as np
import cv2
import time
#import the cascade for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def TakeSnapshotAndSave():
# access the webcam (every webcam has a number, the default is 0)
cap = cv2.VideoCapture(0)
num = 0
while num<10:
# Capture frame-by-frame
ret, frame = cap.read()
# to detect faces in video
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]
x = 0
y = 20
text_color = (0,255,0)
cv2.imwrite('opencv'+str(num)+'.jpg',frame)
num = num+1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
TakeSnapshotAndSave()
Perform imwrite() in the for (x,y,w,h) in faces: loop itself. If you use a constant filename, your last detected face will be saved and the rest will be overwritten