更新:別のスクリプトでプリロードしたいコードの部分を追加してみましたが、スクリプトをインポートしました。しかし、私が思ったように、それはちょうど同じことをしました。どんな助け?私のpythonスクリプトを2つに分割し、それらを1つのように振る舞う方法は?
私はかなり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を言わせて)、その後、ちょうどあなたの2番目のスクリプトで...
import preload
...
それをインポートして、あなただけの最初のスクリプトから変数を参照することができることができると思い
インデントを修正してください。Pythonではインデントを修正してください。 –
申し訳ありません、これは初めてスタックを使用しています。私のコードは実際には字下げされていますが、ここにコピーしたとき何かが間違っていると思います。 –