How can I make Python check which pixel a face is in? - python

import cv2
import pandas as pd
import win32api, win32con
import time
from PIL import ImageGrab
ImageBox = None
im = ImageGrab.grab(ImageBox)
im.save('screenshot.png')
im.close()
image = cv2.imread("screenshot.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=3,
minSize=(30, 30)
)
print("Found {0} Faces!".format(len(faces)))
This is the code I currently have and I need it to find the pixel coordinate of the face.

Related

Using hand pose coordinates (landmarks) from python in Blender for visualisation

I am doing a project to visualise hand movement from a wearable device (gloves) to a screen. I am using Mediapipe to get hand landmarks. This is to get synthetic data for my project. Now I am planning to use Blender to visualise and animate the hand movement. My questions are as follows:
Is there any alternatives for this method?
How to I import these points (as a csv file)and map them on to my blender image?
Note: I am a novice in both Python and Blender.
Any and all help would be appreciated . Thanks in advance.
I used Mediapipe to get the coordinates.
my code for Mediapipe:
'''
import cv2
import mediapipe as mp
import time
import uuid
import os
import numpy as np
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1)
mpDraw = mp.solutions.drawing_utils
pTime = 0
cTime = 0
while True:
success, img = cap.read()
#img = cv2.resize(img, (680,420))
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
imgRGB.flags.writeable = False
results = hands.process(imgRGB)
imgRGB.flags.writeable = True
imgRGB = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
print(results.multi_hand_landmarks)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
#to get width and height of the image
h, w, c = img.shape
#to get center points
cx,cy = int(lm.x*w), int(lm.y*h)
print(id, cx, cy)
#lms= lm.append([id, cx, cy])
if id == 0:
cv2.circle(img, (cx,cy), 15, (255,0,255),cv2.FILLED)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS,
mpDraw.DrawingSpec(color=(201,122,76), thickness=2, circle_radius=2),
mpDraw.DrawingSpec(color=(121,44,250), thickness=4, circle_radius=4),)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255 ,125,0),2)
#cv2.imwrite(os.path.join('Output Images', '{}.jpg'.format(uuid.uuid1())),img)
cv2.imshow('Image', img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
''''

Why does Tesseract not recognize the text on the licence plate while easyocr does?

I am working on automatic licence plate recognition. I could cropped the Plate from inital image. But, Tesseract does not recognize the text on this plate while easyocr does. What is the reason? Thanks in advance for the answers. I used the code extract the plate from a car and recognize.
import cv2 as cv
import pytesseract
import imutils
import numpy as np
import easyocr
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img3 = cv.imread("4.png")
cv.imshow("Car", img3)
img3 = cv.cvtColor(img3, cv.COLOR_BGR2RGB)
gray = cv.cvtColor(img3, cv.COLOR_RGB2GRAY)
bfilter_img3 = cv.bilateralFilter(img3, 11, 17, 17)
edged_img3 = cv.Canny(bfilter_img3, 30, 200)
keypoints = cv.findContours(edged_img3.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(keypoints)
contours = sorted(contours, key=cv.contourArea, reverse=True)[:10]
location = None
for contour in contours:
approx = cv.approxPolyDP(contour, 10, True)
if len(approx) == 4:
location = approx
break
mask = np.zeros(gray.shape, np.uint8)
new_img = cv.drawContours(mask, [location], 0, 255, -1)
new_img = cv.bitwise_and(img3, img3, mask=mask)
print(location)
cv.imshow("Plate", new_img)
(x,y)=np.where(mask==255)
(x1,y1)=(np.min(x),np.min(y))
(x2,y2)=(np.max(x),np.max(y))
cropped_img=gray[x1:x2+1, y1:y2+1]
ret, cropped_img=cv.threshold(cropped_img,127,255,cv.THRESH_BINARY)
cv.imshow("Plate3", cropped_img)
cropped_img = cv.resize(cropped_img, None, fx=2/3, fy=2/3, interpolation=cv.INTER_AREA)
#"cropped_img= the plate image in the question"***********
text = pytesseract.image_to_string(cropped_img)
print("Text by tesseract: ",text)
""""
reader=easyocr.Reader(['en'])
text2=reader.readtext(cropped_img)
print(text2)
"""
k = cv.waitKey(0)
I'm kind of curious why did you use bilateralFilter, Canny, findContours etc.? Did you see the result of each method?
Anyway, if you set the page-segmentation-mode to 6 which is:
Assume a single uniform block of text.
The result will be:
34 DUA34
Code:
import cv2
import pytesseract
# Load the image
img = cv2.imread("vHQ5q.jpg")
# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# OCR
print(pytesseract.image_to_string(gry, config="--psm 6"))
# Display
cv2.imshow("", gry)
cv2.waitKey(0)
You should know the Page segmentation method.
I've got the result using pytesseract-version-0.3.7.
You can try PaddleOCR, they publish a license plate recognition model and also provide a training guide for license plate recognition.
link: https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/applications/%E8%BD%BB%E9%87%8F%E7%BA%A7%E8%BD%A6%E7%89%8C%E8%AF%86%E5%88%AB.md

opencv how to use cascade on screen recorder

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()

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)

my print("Found faces", str(len(faces))) not executed

I'm trying to detect multiple faces using opencv in python. I'm doing this in raspbian OS (raspberry Pi 3). Although the code is working properly, i.e, it's detecting a face and drawing a rectangular boundary around the face. It successfully saves the image in my local folder as well. The problem is : the statement print("Found faces", str(len(faces))) isn't working and the console remains blank. What am I missing here or where am I going wrong?
import io
import picamera
import cv2
import numpy
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.hflip = True
camera.capture(stream, format='jpeg')
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
image = cv2.imdecode(buff, 1)
face_cascade = cv2.CascadeClassifier('face1.xml')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print("Found faces", str(len(faces)))
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
cv2.imwrite('result.jpg',image)

Categories