2016-05-04 3 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 

Antwort

0

Ihr Problem ist in compare_images denken Sie, dass es Ihre globale Variable flag ändern wird, aber das ist nicht der Fall, wenn Sie flag=True Inneren tun Sie wirklich eine neue Variable mit dem gleichen Namen im Inneren zu schaffen, verwenden, um eine Rückkehr statt

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 

und in der Schleife

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

oder wenn Sie die Flagge, was Sie brauchen später tun

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

es auch so aussehen wie Sie nicht img_counter oder original oder shopped oder irgendetwas tun ändern sonst, so dass Sie immer die gleiche Sache immer und immer wieder tun, wenn sie unterschiedlich vergleichen

Verwandte Themen