0

我有一个 python 程序,它处理用 Cognex 相机捕获的图像并给我一个结果(OK / NOK)图像,例如我想要的:如果程序向我们显示图像是 OK,绿色 LED 应该打开,如果图像不正常,红色 LED 应亮起,(两个 LED 连接到 CIO micro Cognex 的输出)

best_model = models.load_model("cnn_casting_inspection.hdf5")
def names(number):
    if number == 1:
        return 'OK'
    else:
        return 'NOK'

# cognex's config
ip = "169.254.2.202"
user = 'admin'
password = ''

#out = cv2.VideoWriter('output.mp4',-1, 20.0, (300,300))
while True:
    start_time = time.time()
    tn = telnetlib.Telnet(ip)
    telnet_user = user+'\r\n'
    tn.write(telnet_user.encode('ascii'))
    tn.write("\r\n".encode('ascii'))
    tn.write(b"SE8\r\n")
    ftp = FTP(ip)
    ftp.login(user)
    filename = 'image.bmp'
    rename = 'image_get.bmp'
    lf = open(rename, "wb")
    ftp.retrbinary("RETR " + filename, lf.write)
    lf.close()
    image2 = cv2.imread(rename)
    cv2.imshow('Image', image2)
    print("--- %s seconds ---" % (time.time() - start_time))
    dir_path = "C:/Users/Dell/PycharmProjects/hsd/image_get.bmp"
    img = image.load_img(dir_path, target_size=(200, 200))
    x = np.array(img.resize((300, 300)))
    x = x.reshape(-1, 300, 300, 1)
    res = best_model.predict_on_batch(x)
    classification = np.where(res == np.amax(res))[1][0]
    print(str(res[0][classification] * 100) + '%  This Is ' + names(classification))
    plt.title(names(classification), weight='bold', size=15)
    plt.imshow(img)
4

0 回答 0