Home>
I wanted to do image recognition with tensorflow, so I thought about making a program that cuts out only the face.
I ran the program by referring to this site (https://newtechnologylifestyle.net/opencv_face_trimingu/).
On this site, it was a program that cuts only the face for one image, so if you cut all the images in the file, the program will be
I modified it, but it is an error and I do not understand the problem.
I'm sorry, but I would like to ask for your help.
import cv2
import glob
directory ='D: /program/face/*.jpg' // File with face image
path ='D:/program/facecut /'// Output destination file
HAAR_FILE ='face.xml'
cascade = cv2.CascadeClassifier (HAAR_FILE)
c = 0
for image_picture in glob.glob ('./face/*.jpg'):
img = cv2.imread (image_picture)
img_g = cv2.imread (image_picture, 0)
face = cascade.detectMultiScale (img_g)
print (face)
for x, y, w, h in face:
face_cut = img [y: y + h, x: x + w]
c + = 1
filename = path +'face_cut {} .jpg'.format (c)
cv2.imwrite (filename, face_cut)
Traceback (most recent call last):
File "d: /program/OneImageFaceCut.py", line 19, in<module>
cv2.imwrite (filename, face_cut)
NameError: name'face_cut' is not defined
What I tried
for image_picture in glob.glob ('./face/*.jpg'):
img = cv2.imread (image_picture)
img_g = cv2.imread (image_picture, 0)
face = cascade.detectMultiScale (img_g)
print (face)
for x, y, w, h in face:
face_cut = img [y: y + h, x: x + w]
c + = 1
filename = path +'face_cut {} .jpg'.format (c)
cv2.imwrite (filename, face_cut)
In this way, I put out cv2.imwrite () outside the for statement. Then, the compilation passed, and only one image with the face cut out was saved.
I can imagine that only one sheet is output, but I'm not sure if it won't compile when I put it in the for statement.
opencv 4.0.1
-
Answer # 1
Related articles
- python - program error removal with opencv tracker
- python - an error occurs in the if statement program that compares the size of numbers
- python - i got an error in opencv
- python - resolve program error "keyerror: 0"
- python - the py file cannot be executed in the task scheduler 0x2 error
- python - a program that converts ordinal data to paired comparison data (i want to create a winning/losing matrix by turning a d
- python - sklearn, svm error
- python - error in image binarization using cv2adaptivethreshold function
- python - in raspberry pi, the error occurs only in the case of the automatic start program using systemd
- python - categorical_crossentoropy error does not resolve
- readcsv error in python
- python 3x - i want to daemonize a python program, but i want to see if this method works
- python - how to resolve attribute error
- python - i want to display an image with pysimplegui, but an error occurs
- python - error when scraping with selenium and firefox
- [python] graphviz output format error
- python - i get an error when connecting to a voice channel with discordpy
- python 3x - best estimator: i get an error with no syntax, so please tell me what to do
- [python] i don't know how to solve the error
Related questions
- Error starting OpenCV Python
- python : Error starting
- Python OpenCV Color
- How to force the server on Flask to execute my Python code block?
- python : Importerror: DLL LOAD FAILED: The specified module is not found. Will not see CV2.
- python : How to create median image?
- python : Celeba Dataset is incorrect?
- Connecting to IP Camera via OpenCV Python 3.4
- python : How to convert QImage to Mat?
- How to cut grind image via OpenCV on Python?
If no face is detected in the very first image,
for x, y, w, h in face:
Because the loop inside is not executedface_cut
Will go to the save process without being defined.Therefore, an error has occurred.
Also, after being detected even once
face_cut
Since is defined, no error will occur, but the process is such that the data of another image is saved for the image for which the face is not detected.The easiest way to fix it is
c + = 1
After that, it will be put in the for block.