以下程序有什么问题?它运行无限

时间:2016-05-04 00:01:58

标签: python-2.7

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

1 个答案:

答案 0 :(得分:0)

你的问题在compare_images,你认为它会修改你的全局变量flag但事实并非如此,当你在其中flag=True进行操作时,你真的会创建一个新的变量在其中使用相同的名称,使用返回

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_counteroriginalshopped或其他任何事情,所以如果他们比较不同,你会永远一遍又一遍地做同样的事情