I am working on a virtual make-up using Python, openCV, dlib. Currently, I can get the facial landmarks like lips, nose, jaw etc. But I am quite unsure on getting the points of the cheeks.
Are they any recommendations?
If you're using dlib 68 facial landmarks, here are the ROIs of the 2 cheeks:
from imutils import face_utils
#face detection part
#rect is the face detected
shape = predictor(gray_img, rect)
shape = face_utils.shape_to_np(shape)
img[shape[29][1]:shape[33][1], shape[54][0]:shape[12][0]] #right cheeks
img[shape[29][1]:shape[33][1], shape[4][0]:shape[48][0]] #left cheek
Related
I am trying to detect a black tape on a black background.
No tape, with tape (cropped pictures):
I have first cropped the area of the tape from the original image and then performing thresholding on it. Below is the image when there is no tape:
You can notice there is an almost solid line. Black tape is placed right next to it and when it is placed this line becomes very light. Below is the image:
Is there any good image processing techniques I can use to detect when the black tape is placed and when its not placed?
Below is the code I am currently using:
import cv2
import os
import imutils
from pathlib import Path
import numpy as np
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print("X: {} | Y: {}".format(x, y))
dirPath = Path(__file__).parents[2]
imgPath = os.path.join(dirPath, "img", "img.png")
win_name = "Image"
cv2.namedWindow(win_name)
cv2.setMouseCallback(win_name, on_mouse)
img = cv2.imread(imgPath)
img = imutils.resize(img, width=800)
roiImg = img[298:337, 520:591]
img_gray = cv2.cvtColor(roiImg, cv2.COLOR_BGR2GRAY)
rett, thresh = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)
cv2.imshow(win_name, img)
cv2.imshow("Thres", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here is the link to test video: https://drive.google.com/file/d/1P3Xkx_SuHidDs1UdacS3-DZqA-CiXQOX/view?usp=sharing
Below is the image with area marked in red where tape is usually placed
Thanks
No way to write a stable image processing software here.
In this industrial environment you get differences in ambient light, reflexion, shadows, light different presentation angles, sun light etc. This will impact the brightness of your image partial or global much more than the presence of a nearly invisible tape. This way it's not possible to find any good threshold.
So I guess you are on the right way using the "temporary solution" of detecting the gray hand.
If you really like to detect the tape you need a hardware solution that brings you away from this black on black thing:
Use white tape on black part or black tape on white part. Only to mention :-)
Use dark/bright field illumination instead of ambient light. Guess will not work because angle of part and tape is similar.
Use different wavelength with more difference than in visible light. Needs specific camera and illumination. Best wavelength depends a lot of the material but that's the most professional and stable solution here.
I am trying to do face detection but it does not detect any face.
this is the function I have created for face detection
def faceDetection(test_img):
gray_img=cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)
face_haar_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# haar classifier
faces=face_haar_cascade.detectMultiScale(gray_img,scaleFactor=1.32,minNeighbors=5)
return faces,gray_img
this is used in
test_img=cv2.imread('pic.png')
faces_detected,gray_img=fr.faceDetection(test_img)
print("faces_detected:",faces_detected)
for (x,y,w,h) in faces_detected:
cv2.rectangle(test_img,(x,y),(x+w,y+h),(255,0,0),thickness=5)
resized_img=cv2.resize(test_img,(500,500))
cv2.imshow("face",resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows
but when I run this script it does not show any face detected
simply give output this
faces_detected: ()
and no box around image
Try using a different haar cascade. The default one is haarcascade_frontalface_alt.xml
face_haar_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
Change the scale factor you use for the cascade. If that doesn't work you can also reduce also the number of neighbors to maybe 2.
faces = face_haar_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5);
Check the number of faces you found
print('Faces found: ', len(faces))
I am busy working on some very simple vehicle detection software using Python and OpenCV. I want to take a screen capture on the moment an object hits the line I have created.
Searching on Google resulted in nothing or some very big C++ projects. Since I am very unskilled with C++ I tought I would try to ask it here.
My code:
import cv2
face_cascade = cv2.CascadeClassifier('cars.xml')
vc = cv2.VideoCapture('dataset/traffic3.mp4')
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
cv2.line(frame, (430, 830), (430, 100),(0,255,0), 3)
cv2.line(frame, (700, 700), (700, 100),(0,0,255), 3)
cv2.imshow("Result",frame)
cv2.waitKey(1);
vc.release()
So I want to take a screencapture the moment a vehicle passes on of the 2 lines?
Can somebody help me?
Thanks.
OpenCV's Cascade classifier will return a collection of Rect objects that correspond to bounding boxes around each car it detected in the image. If you don't know how to use the classifier, look at this tutorial in C++ to get an idea of how it works. Translating it to Python shouldn't be too hard.
Once you have these bounding boxes, you only need to test whether they intersect one of your lines to detect vehicles passing on the line.
I'm currently making a game in pygame, Python 3 and a part of the code that has been giving me issues is:
for counter in range(0, 30):
particles = pygame.image.load('particles.png').convert()
particles = pygame.transform.rotozoom(particles, 36*counter, 1.1**counter).convert()
particles.set_colorkey((0, 0, 0, 0))
screen.blit(particles, particles.get_rect(centerx=480, centery=100))
pygame.display.flip()
time.sleep(0.05)
particles.png is just a few colored pixels on a transparent background. The problem is that when the image is rotated and scaled, some of those particles sort of blur out resulting in a mass of black squares around them.
How do I fix this problem? Thanks in advance!!
I've had a bad experience with pygame's rotozoom combined with transparency. Instead of loading the image and then rotozooming it, consider the following:
Using PIL library:
Convert the loaded surface to a string using pygame.image.tostring
Convert the string to an Image object
Use PIL's functions to replace rotozoom (zooming, rotating)
Convert the Image object back into a surface using the same method
Using math:
Load data about the position and color of the particles,
or infer it from the loaded surface
Use simple mathematical functions to caculate their new position,
according to the angle and zoom
Paint them using pygame.gfxdraw, pygame.draw or pygame.Surface.set_at
Good luck!
I'm working on a short intro in PyGame, and I need to blur some static lines. After two days of searching, I can't find anything.
Does PyGame have a built-in method to blur a shape or surface? Will I need to do it manually? What would be the best way to approach this?
To clarify, I'm not looking for motion blur – I just need a simple in-place blur, almost like a glowing effect.
There's no built-in way to blur a Surface.
Workarounds are:
scale the Surface with pygame.transform.smoothscale and then back to its original size (ugly)
iterating over each pixel, get the color of the neighbour pixels, calculate the average value for red, green, and blue, and set the color of that pixel (slow as hell if not using something like numpy)
I recommend using the Python Imaging Library (PIL), since it's nice and fast.
Here's a simple and running example:
import pygame
from PIL import Image, ImageFilter
pygame.init()
screen = pygame.display.set_mode((512, 512))
clock = pygame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nwjr/qrWfZM1Rd0+oKyeuP3Qw39Qc7OJf+ILZJ4Hx0HJvgHwA4lQKxrbXYuanU3803YAfzDN2ICJetTsbOSfFuYJFtvCiJqd7fyztoD6Ed33CSgW27oe2/inpfteAYVi8QxX1/hDDNiKiOU9Hbbzl11LphQ1Pxf5W7ZfcJuaoWv8t27vjPNuEHLABPs0yahLzdAl/jvntuXm3GALDFjnKaBm6AJ/1A8ffuXVm3K6qAVBrpd438Yy9agZ2s7f8udEDagZ2szftjUBz1+dLBvne/4KJXK/mCXy/OvEfMH7hSyQ55/j1xHXMa5DTbjDfR9ie28/Bf+88Rz2fcGaDvK6NtaBVCigZsiFv+XzuLqyeg1YJX+HfLqMWtQMufBnwMK0htT8OPFv4DP+fP1HOv67M6cTlZX3/OniP77npzk+YEDNjht/CPu8NmQc2KZmx5F/Q2zAib6vi38DbMD6uK+b/9gG9szbem9HmQJqZrbwT+RQXnBAzctG/hDquyyfG2CPF5Z7eNvAH8L80NIcgZPsTfNPjwssus/PWfYx/75p/umYUPVceIwf6zw7XpF6LrOP+QdU/Cf+IJorYowIzonwM15P+4xj59+8Zog79vKzel3XJv4SGsR9Uif3wPU+bzN/fOaITzdmpYr7sGncbeYf28BMzGw4BfNeU/y8a/zTihiujX0CeA4K1I/tZfaI5TU7nr+X5+/l+Xt5/l6ev9cU/GcZcPX8aW2gx4CtiKy+146zFvnHgVEkZ+quOCpq39Yig/1gcgT/1LjcLKEdtJnEBNhim7o9mir0uUhdwz4Bfv4cfBH19/e6yRYwVwhiexgp7ueB7+v2KY4V7Zhfon7MNKte6j04x4/nJPUfe7dmpQ=='
size, image_mode, raw = (128, 128), 'RGBA', compressed.decode("base64").decode("zlib")
# create the original pygame surface
surf = pygame.image.fromstring(raw, size, image_mode)
# create a PIL image and blur it
pil_blured = Image.fromstring("RGBA", size, raw).filter(ImageFilter.GaussianBlur(radius=6))
# convert it back to a pygame surface
other = pygame.image.fromstring(pil_blured.tostring("raw", image_mode), size, image_mode)
pygame.time.set_timer(pygame.USEREVENT, 1000)
while True:
for e in pygame.event.get():
if e.type == pygame.USEREVENT: surf, other = other, surf
if e.type == pygame.QUIT: break
else:
screen.fill((255, 255, 255))
screen.blit(surf, (192, 192))
pygame.display.flip()
clock.tick(60)
continue
break
Hint: If you're on Windows and want to install PIL, download it from here and install it via
pip install Pillow-2.8.2-cp27-none-win32.whl
or whatever version you downloaded (installing PIL on Windows can be a great PITA otherwise).