python - 将其他 Python 脚本作为模块导入
问题描述
我目前正在尝试编写一个应用程序,将视频分割成单独的帧,然后在视频中找到人脸并将它们提取为.jpg。我将项目拆分为多个文件,app.py 负责 GUI 等,extractor.py 负责完成工作。
我以为您可以使用以下命令导入文件:
import extractor
然后像这样运行它:
extractor()
显然,这似乎不起作用。我还尝试使整个提取器脚本成为一个函数,然后调用该函数,但这也不起作用。
应用程序.py:
import 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.")
我收到错误:TypeError: 'module' object is not callable。如果我按照你们中的某些人的建议或标记为“类似”的问题执行此操作,则脚本将启动,但它仍然无法工作,只能创建文件夹。
解决方案
调用它,extractor.main()
因为您不必向它传递任何参数。