I wanted to try trimming and saving the face part of the image, and I tried to use the code that was published by someone else, but I got an error and I can't execute it. Is something wrong?
I am sorry if this is a rudimentary question, but please give me a lecture.
Applicable source code
C: \ Users \ kiara \ PycharmProjects \ openCV \ venv \ Scripts \ python.exe C: /Users/kiara/PycharmProjects/openCV/img_cuts.py Namespace (cascade = 'alt', input_dir = './Input /', min = 80, move_dir = '/ done /', neighbors = 2, scale = 1.3) Traceback (most recent call last): File "C: \ Users \ kiara \ Anaconda3 \ lib \ shutil.py", line 544, in move os.rename (src, real_dst) FileExistsError: [WinError 183] Cannot create a file that already exists. : './input/00000001.jpg'->'./input//done/' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C: /Users/kiara/PycharmProjects/openCV/img_cuts.py", line 124, in<module> shutil.move (FLAGS.input_dir + file_name, FLAGS.input_dir + FLAGS.move_dir) File "C: \ Users \ kiara \ Anaconda3 \ lib \ shutil.py", line 558, in move copy_function (src, real_dst) File "C: \ Users \ kiara \ Anaconda3 \ lib \ shutil.py", line 257, in copy2 copyfile (src, dst, follow_symlinks = follow_symlinks) File "C: \ Users \ kiara \ Anaconda3 \ lib \ shutil.py", line 121, in copyfile with open (dst, 'wb') as fdst: OSError: [Errno 22] Invalid argument: './input//done/' Process finished with exit code 1
import cv2, os, argparse, shutil # Directory for cropped images SAVE_PATH = "./outputs/" # Basic model parameters FLAGS = None # Types of trained models CASCADE = ["default", "alt", "alt2", "tree", "profile", "nose"] # Pass if executed directly (imported but not executed) if __name__ == "__main__": parser = argparse.ArgumentParser () parser.add_argument ( "--cascade", type = str, default = "alt", choices = CASCADE, help = "cascade file." ) parser.add_argument ( "--scale", type = float, default = 1.3, help = "scaleFactor value of detectMultiScale." ) parser.add_argument ( "--neighbors", type = int, default = 2, help = "minNeighbors value of detectMultiScale." ) parser.add_argument ( "--min", type = int, default = 80, help = "minSize value of detectMultiScale." ) parser.add_argument ( "--input_dir", type = str, default = "./ input /", help = "The path of input directory." ) parser.add_argument ("--move_dir", type = str, default = "/ done /", help = "The path of moving detected files." ) # Get and execute parameters FLAGS, unparsed = parser.parse_known_args () # Classifier directory (obtained from below) # https://github.com/opencv/opencv/blob/master/data/haarcascades/ # https://github.com/opencv/opencv_contrib/blob/master/modules/face/data/cascades/ # Trained model file if FLAGS.cascade == CASCADE : # "default": cascade_path = "C: /Users/kiara/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml" elif FLAGS.cascade == CASCADE : # "alt": cascade_path = "C: /Users/kiara/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_alt.xml" elif FLAGS.cascade == CASCADE : # "alt2": cascade_path = "C: /Users/kiara/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_alt2.xml" elif FLAGS.cascade == CASCADE : # "tree": cascade_path = "C: /Users/kiara/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_alt_tree.xml" elif FLAGS.cascade == CASCADE : # "profile": cascade_path = "C: /Users/kiara/Desktop/opencv-master/data/haarcascades/haarcascade_profileface.xml" elif FLAGS.cascade == CASCADE : # "nose": cascade_path = "C: /Users/kiara/Desktop/opencv_contrib-master/modules/face/data/cascades/haarcascade_mcs_nose.xml" # Get the features of cascade classifier faceCascade = cv2.CascadeClassifier (cascade_path) # Number of successful face detection (default 0) face_detect_count = 0 # Number of face detection failures (0 by default) face_undetected_count = 0 # Store the files in the folder in a variable (also stores the directory) files = os.listdir (FLAGS.input_dir) # If i have not moved the success file, delete and recreate it if the output directory exists if FLAGS.move_dir == "": if os.path.exists (SAVE_PATH): shutil.rmtree (SAVE_PATH) os.mkdir (SAVE_PATH) print (FLAGS) # If a face is detected from the collected image data, cut and save it. for file_name in files: # For files (not directories) if os.path.isfile (FLAGS.input_dir + file_name): # Read image file img = cv2.imread (FLAGS.input_dir + file_name) # Since there is a file that rarely fails when there are a lot of images, log output and skip (cause unknown) if img is None: print (file_name + ': Cannot read image file') continue # Convert from color to grayscale (because face is not detected in color) gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # Face detection face = faceCascade.detectMultiScale (gray, scaleFactor = FLAGS.scale, minNeighbors = FLAGS.neighbors, minSize = (FLAGS.min, FLAGS.min)) if len (face)>0: for rect in face: # Cut image output cv2.imwrite (SAVE_PATH + str (face_detect_count) + file_name, img [rect : rect  + rect , rect : rect  + rect ]) face_detect_count = face_detect_count + 1 # Move detected files if FLAGS.move_dir! = "": shutil.move (FLAGS.input_dir + file_name, FLAGS.input_dir + FLAGS.move_dir) else: print (file_name + ': No Face') face_undetected_count = face_undetected_count + 1 print ('Undetected Image Files:% d'% face_undetected_count)
Answer # 1
You cannot create a file that already exists.
The problem is that the file
input/done/00000001.jpgexists at the destination.
*) The reason why this event occurs will be described later.
Is it better to use Path # replace as a countermeasure?
Below is the untested code.
from pathlib import Path # Foreword # If you have not moved the success file, delete and recreate it if the output directory exists if FLAGS.move_dir == "": if os.path.exists (SAVE_PATH): shutil.rmtree (SAVE_PATH) os.mkdir (SAVE_PATH) else: # add this line move_dir = Path (FLAGS.input_dir, FLAGS.move_dir) # create input/done directory move_dir.mkdir (parents = True, exist_ok = True) # Abbreviation # If a face is detected from the collected image data, cut and save it. for file_name in files: src_file = Path (FLAGS.input_dir, file_name) # For files (not directories) if src_file.is_file (): # Read image file img = cv2.imread (str (src_file)) # Abbreviation # Move detected files if FLAGS.move_dir! = "": src_file.replace (move_dir/file_name)
shutil.move ()internally calls
os.rename ()when another file is renamed./Linux) and operation is different.
File"C: \ Users \ kiara \ Anaconda3 \ lib \ shutil.py" ;, line 544, in move
os.rename (src, real_dst)
It is speculation, but the person who wrote the sample code performs an operation test on
linux, and the questioner works on
WindowsI wonder if this happened because of this.
Path # replaceto perform rename processing in a cross-platform environment in the Python language.
◆ Reference information
Os.rename Bold characters are quoted
On Unix, if dst existsand the file isimplicitly replaced as long as the user has permission.This operation may fail on some Unix-like systems if src and dst are on different file systems. If the rename is successful, it is an atomic operation (this is a POSIX requirement).On Windows, if dst already exists,OSErroris raised, even for files.
- php - about saving and displaying images in laravel
- python - i don't understand the opencv error
- python - opencv freezes
- can't collect images from google with python icrawler
- human detection from images using python and opencv
- python - about opencv color information acquisition
- questions about batch filtering of images in folders using opencv (python3)
- python 3x - displaying images in pygame
- python - creating correct image file in opencv cascade classifier
- get camera image with python opencv
- [opencv for unity] i would like to change the codec when saving a video with the videowriter function to mp4 or avi
- how to search the image with python opencv and output the judgment result
- python - [opencv] error: (-215:assertion failed) cv2merge()
- python 3x - i want to display two jpg images in python and display different images with the buttons
- python - i want to limit the number of images to be extracted
- python - about error when reading images of self-made image classifier
- python newbie opencv region extractor
- python 3x - i want to save multiple images by scraping, but i'm not sure why no site can
- opencv - i want to resize multiple loaded images and save them with the same name
- python opencv area extraction beginner
- python - please tell me the solution of yolov3 error [filenotfounderror: [errno 2] no such file or directory:'traintxt']
- python - image recognition using the created cascade classifier
- python - modelpredict() cannot be executed in tensorflow
- python - [natural language processing] regarding back propagation of embedding layer when word2vec is implemented
- python - i want to convert the daytime image like an infrared image to use for learning yolov3
- in python dataframe, i want to find the average value of those that meet specific conditions in index units
- python - 'functional' object has no attribute'predict_classes' cannot be fixed
- python - how to exit the infinite loop while leaving the output of jupyter notebook
- python - to detect a person from the loaded image
- python - on the implementation of the softmax function for logistic regression