python - 如何将我的 python 脚本分成两个并让它们像一个一样运行?

标签 python xml multithreading opencv

更新:我尝试在不同的脚本中添加我想要预加载的代码部分,然后我导入了脚本。但正如我所想的那样,它只是做了同样的事情。有帮助吗?

所以我对 python 和 opencv 还很陌生,我想知道你是否可以帮我做点什么。我的 python 脚本有一个部分需要 22 秒才能加载(XML 文件)。我怎样才能将该部分放入另一个脚本并仍然使用 XML 文件加载到的变量。我想要这个是因为我需要即时的东西......而且我只需要在将新用户添加到面部识别算法时加载 XML。

完整脚本:

import cv2

import config
import face




# Load training data into model
print 'Loading training data...'
model = cv2.face.createEigenFaceRecognizer()
model.load(config.TRAINING_FILE)
print 'Training data loaded!'
# Initialize camer and box.
camera = config.get_camera()



print 'Press Ctrl-C to quit.'

while True:
# Check if capture should be made.


print 'looking for face...'
# Check for the positive face and unlock if found.
image = camera.read()
# Convert image to grayscale.
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Get coordinates of single face in captured image.
result = face.detect_single(image)
if result is None:
    print 'Could not detect single face!  Check the image in capture.pgm' \
          ' to see what was captured and try again with only one face            visible.'
continue
x, y, w, h = result
# Crop and resize image to face.
crop = face.resize(face.crop(image, x, y, w, h))
# Test face against model.


label, confidence = model.predict(crop)
print 'Predicted {0} face with confidence {1} (lower is more    confident).'.format(
    'POSITIVE' if label == config.POSITIVE_LABEL else 'NEGATIVE',
    confidence)
if label == config.POSITIVE_LABEL: 
    print 'Recognized face!'
else:
    print 'Did not recognize face!'

我想在单独的脚本中预加载部分以便节省时间:

# Load training data into model
print 'Loading training data...'
model = cv2.face.createEigenFaceRecognizer()
model.load(config.TRAINING_FILE)
print 'Training data loaded!'

我希望能够使用模型,但不必每次都加载。所以,我只会加载一次,然后立即运行脚本的另一部分。

请帮助我,谢谢。

最佳答案

我想你可以用第一部分创建另一个脚本(比如 preload.py),然后将它导入你的第二个脚本

...
import preload
...

然后您可以通过使用以下命令从第一个脚本中引用变量:

preload.model

关于python - 如何将我的 python 脚本分成两个并让它们像一个一样运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017117/

相关文章:

python - Django 内部服务器错误而不是 404

python - 如何在 Linux 下读取 GPS 数据?

java - 使用 DOM 解析器将 XML 文件输出到文本文件?

python - 即使当前队列包含未处理的元素,如何通知python中的queue.join()停止阻塞?

C++:如何简化线程锁?

objective-c - NSInvocationOperation 等价 yield

python - Keras Conv1D 步骤参数

python - Seaborn Lineplot 显示自定义中心线而不是均值

java - Android Studio 找不到属性 XMLConstants.ACCESS_EXTERNAL_DTD

java - JAXB:将 Javabean 对象转换为 XML,所有属性作为 XML 属性而不是 XML 元素(节点)