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

时间:2017-02-03 05:02:54

标签: python xml multithreading opencv

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

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

完整脚本:

<?php

        $email;$comment;$captcha;
        if(isset($_POST['email']))
          $email=$_POST['email'];
        if(isset($_POST['comment']))
          $comment=$_POST['comment'];
        if(isset($_POST['g-recaptcha-response']))
          $captcha=$_POST['g-recaptcha-response'];

        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
        if($response['success'] == false)
        {
          echo '<h2>You are spammer ! Get the @$%K out</h2>';
        }
        else
        {
          echo '<h2>Thanks for posting comment.</h2>';
        }
?>

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

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!'

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

请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:0)

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

...
import preload
...

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

preload.model