-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
imread() didn't close the image file sometime #12342
Copy link
Copy link
Closed
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/https://opencv.org/opencv-hackathon-starts-next-week/bugcategory: imgcodecsplatform: win32test
Description
I have a program where I read an image file (e.g. from a webcam), crop a face (if any) and save the cropped face into a new image file. The program will then delete the original image file. Here's the code:
import cv2
import os
import datetime
cam = cv2.VideoCapture(0)
while True:
imagenames = []
imagename = 'face'+str(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))+'.jpg'
ret, frame = cam.read()
cv2.imwrite(imagename, frame)
img = cv2.imread(imagename)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
n = 0
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
crop_img = img[y:y+h, x:x+w]
try:
imagenames.append(str(n)+'_'+imagename)
cv2.imwrite("img"+str(n)+'_'+imagename,crop_img)
n=n+1
except Exception, e:
print e
pass
os.remove(imagename)
Problem is, sometime the image is not properly closed. This cause the program to throw this error:
The process cannot access the file because it is being used by another process
opencv-python version: 3.4.2.17
OS: Windows 7
Python version: 2.7.15
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/https://opencv.org/opencv-hackathon-starts-next-week/bugcategory: imgcodecsplatform: win32test