2016-05-04 9 views
0
from skimage.measure import structural_similarity as ssim 
import matplotlib.pyplot as plt 
import numpy as np 
import cv2 
import time 

img_counter=0 
flag=False 

def mse(imageA, imageB): 
    # the 'Mean Squared Error' between the two images is the 

    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) 
    err /= float(imageA.shape[0] * imageA.shape[1]) 


    return err 

def compare_images(imageA, imageB): 
    # compute the mean squared error and structural similarity 
    # index for the images 
    m = mse(imageA, imageB) 
    s = ssim(imageA, imageB) 

    if m > 150 or s < 0.90: 
     print "object is detected" 
     flag=True 

while True: 

    original = cv2.imread("/home/lingesh/last_try/images/0.jpg") 
    shopped = cv2.imread("/home/lingesh/last_try/images/{}.jpg".format(img_counter+1)) 
    # convert the images to grayscale 
    original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) 
    shopped = cv2.cvtColor(shopped, cv2.COLOR_BGR2GRAY) 
    compare_images(original, shopped) 
    if flag==True 
     break 

答えて

0

あなたの問題は、あなたはそれがあなたのグローバル変数flagを変更しますが、そうではないと思われるcompare_imagesにある無限に実行されますあなたは本当にその中に同じ名前で新しい変数を作成し、あなたはフラグ事が必要な場合は、代わりに

def compare_images(imageA, imageB): 
    # compute the mean squared error and structural similarity 
    # index for the images 
    m = mse(imageA, imageB) 
    s = ssim(imageA, imageB) 

    if m > 150 or s < 0.90: 
     print "object is detected" 
     return True 
    return False 

、ループ内

while True: 
    #code 
    if compare_images(original, shopped): 
     break 

以降に

を行うリターンを使用します
while True: 
    #code 
    flag = compare_images(original, shopped) 
    if flag: 
     break 

あなたは永遠に何度も何度も同じことをやってますので、それらが異なる

を比較する場合は、 img_counterまたは originalまたは shoppedまたは何か他のものを変更しないようにもそれが見えます
関連する問題