gpt4 book ai didi

python - 将其他 Python 脚本作为模块导入

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:20 24 4
gpt4 key购买 nike

我目前正在尝试编写一个应用程序,将视频分割成单独的帧,然后找到视频上的面孔并将其提取为 .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:“模块”对象不可调用。如果我按照你们中的一些人的建议或标记为“类似”的问题中的方式进行操作,脚本将启动,但它仍然无法工作,只能创建文件夹。

最佳答案

您可以通过将 if __name__ == "__main__": 转换为新函数 def extractor() 并导入来运行 extractor.py模块:

import extractor;
extractor.extractor();

您还可以使用以下导入变体仅导入特定名称(在我们的示例中为 extractor() 函数):

from extractor import extractor;
extractor();

查看此链接 ( https://repl.it/repls/MeaslyMerrySymbol ),我在其中完成了与您的文件类似的示例导入。

关于python - 将其他 Python 脚本作为模块导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56955196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com