import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
#import datetime
#start = datetime.datetime.now()#
import time
start = time.process_time()


count = 1000
record = 0
for x in range(count):
    img = np.ones((1, 60), dtype=np.uint8)
    img[0, range(0, 20)] = 30
    img[0, range(20, 40)] = 125
    img[0, range(40, 60)] = 250
    filename = 'make' + str(x) + '.bmp'
    print(filename)
    cv2.imwrite(filename, img)

    # 讀取圖檔
    image = cv2.imread(filename)
    # 轉為灰階
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    p = 0
    d = 0
    tt = np.array(gray[0])
    count = len(tt)


    for i in tt[:]:
        if i >= 150:
            p = p + 1
        elif i <= 130:
            d = d + 1
    print('要得豆子像素:', p)
    print('壞的豆子像素:', d)
    coffeebean = (d / count) * 100
    print('壞豆佔全部百分比:', coffeebean)
    if coffeebean > 60:
        record = record + 1
        print('紀錄次數:', record)

    if record == 50:
        j = 1
        print('噴嘴啟動')

# 結束測量
end = time.process_time()
# 輸出結果
print("執行時間:%f 秒" % (end - start))