I am currently trying to program an application that splits a video into its individual frames and then finds the faces on the video and extracts them as .jpg. I split the Project into multiple files, app.py which is responsible for GUI and so on and extractor.py which does the work.
I thought you would be able to import a file with:
import extractor
And then run it like this:
extractor()
Apparently, this doesnt seem to work. I also tried making the entire extractor script a function and then calling the function, but this also doesnt work.
app.py:
import extractor
extractor()
extractor.py:
import cv2
import os
import face_recognition
from PIL import Image
import multiprocessing
try:
if not os.path.exists('frames'):
os.makedirs('frames')
except OSError:
print('Error: Creating directory of frames')
try:
if not os.path.exists('faces'):
os.makedirs('faces')
except OSError:
print('Error: Creating directory of faces')
def extract_frames(video_file_path):
currentFrame_extract = 1
video_capture = cv2.VideoCapture(video_file_path)
while(True):
ret, frame = video_capture.read()
if ret == False:
break
name = 'frames/frame_' + str(currentFrame_extract) + '.jpg'
print(f"Extracting Frame {currentFrame_extract}, saving it as Frame_{currentFrame_extract}.jpg")
cv2.imwrite(name, frame)
currentFrame_extract += 1
video_capture.release()
cv2.destroyAllWindows()
return currentFrame_extract
def find_faces_a(a):
i = 0
currentFrame = 1
while (True):
if a > currentFrame:
image = face_recognition.load_image_file(f"data/frame_{currentFrame}.jpg")
face_locations = face_recognition.face_locations(image)
if len(face_locations) >= 1:
top, right, bottom, left = face_locations[0]
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(f"faces/face_{currentFrame}.jpg".format(i))
print(f"Found a face at Frame_{currentFrame}, exporting it as Face_{currentFrame}.jpg")
currentFrame += 4
else:
break
def find_faces_b(a):
i = 0
currentFrame = 2
while (True):
if a > currentFrame:
image = face_recognition.load_image_file(f"data/frame_{currentFrame}.jpg")
face_locations = face_recognition.face_locations(image)
if len(face_locations) >= 1:
top, right, bottom, left = face_locations[0]
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(f"faces/face_{currentFrame}.jpg".format(i))
print(f"Found a face at Frame_{currentFrame}, exporting it as Face_{currentFrame}.jpg")
currentFrame += 4
else:
break
def find_faces_c(a):
i = 0
currentFrame = 3
while (True):
if a > currentFrame:
image = face_recognition.load_image_file(f"data/frame_{currentFrame}.jpg")
face_locations = face_recognition.face_locations(image)
if len(face_locations) >= 1:
top, right, bottom, left = face_locations[0]
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(f"faces/face_{currentFrame}.jpg".format(i))
print(f"Found a face at Frame_{currentFrame}, exporting it as Face_{currentFrame}.jpg")
currentFrame += 4
else:
break
def find_faces_d(a):
i = 0
currentFrame = 4
while (True):
if a > currentFrame:
image = face_recognition.load_image_file(f"data/frame_{currentFrame}.jpg")
face_locations = face_recognition.face_locations(image)
if len(face_locations) >= 1:
top, right, bottom, left = face_locations[0]
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(f"faces/face_{currentFrame}.jpg".format(i))
print(f"Found a face at Frame_{currentFrame}, exporting it as Face_{currentFrame}.jpg")
currentFrame += 4
else:
break
if __name__ == "__main__":
video_file_path = "Video_3.mp4"
currentFrame_extract = extract_frames(video_file_path)
currentFrame_extract = [currentFrame_extract]
p1 = multiprocessing.Process(target=find_faces_a, args=(currentFrame_extract))
p2 = multiprocessing.Process(target=find_faces_b, args=(currentFrame_extract))
p3 = multiprocessing.Process(target=find_faces_c, args=(currentFrame_extract))
p4 = multiprocessing.Process(target=find_faces_d, args=(currentFrame_extract))
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
print("Frame extraction and alignment finished successfully.")
I receive the error: TypeError: 'module' object is not callable. If I do it like some of you have suggested or as in the question marked as "similar", the script will start but it still wont work and only create the folders.