123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #coding=utf-8
- import os
- import numpy as np
- import datetime
- import cv2
- import pymysql
- import time
- import tensorflow as tf
- import requests as req
- from numba import jit
- from urllib import parse
- from PIL import Image
- conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd='g53743001', db='coffee_detection',
- charset='utf8')
- image_size = 150
- cap = cv2.VideoCapture(0)
- def takephoto():
- ret, frame = cap.read()
- frame = cv2.resize(frame, (1936, 1096))
- cv2.imwrite("D:\\fatwolf\\company_files\\opencv\\2.png", frame)
- #cap.release()
- def cut_rectangle():
- #img = cv2.imread("D:\\fatwolf\\company_files\\opencv\\2021-05-05-11_13_47.png")
- img = cv2.imread("D:\\fatwolf\\company_files\\opencv\\2.png")
- # img = cv2.resize(img1,(968,548))
- # img = img.shape
- point_color = (0, 0, 255)
- # roi = img[421:527,328:369]
- command1 = "SELECT Name,X, X1 ,Y ,Y1 FROM `cut` WHERE Name LIKE 'roi1'"
- l = conn.cursor()
- l.execute(command1)
- conn.commit()
- r1 = l.fetchone()
- #print(r1[0])
- command2 = "SELECT Name,X, X1 ,Y ,Y1 FROM `cut` WHERE Name LIKE 'roi2'"
- l = conn.cursor()
- l.execute(command2)
- conn.commit()
- r2 = l.fetchone()
- #print(r2[0])
- command3 = "SELECT Name,X, X1 ,Y ,Y1 FROM `cut` WHERE Name LIKE 'roi3'"
- l = conn.cursor()
- l.execute(command3)
- conn.commit()
- r3= l.fetchone()
- #print(r3[0])
- command4 = "SELECT Name,X, X1 ,Y ,Y1 FROM `cut` WHERE Name LIKE 'roi4'"
- l = conn.cursor()
- l.execute(command4)
- conn.commit()
- r4 = l.fetchone()
- #print(r4[0])
- def roi1():
- x = r1[1]
- x1 = r1[2]
- y = r1[3]
- y1 = r1[4]
- i = 1
- number = 1
- # roi [y:y1,x:x1]
- # rectangle (x.y),(x1,y1)
- roi = img[y:y1, x:x1]
- cv2.rectangle(img, (x, y), (x1, y1), point_color, 1)
- roi = cv2.resize(roi,(image_size, image_size), 0, 0, cv2.INTER_LINEAR)
- cv2.imwrite('D:\\fatwolf\\company_files\\paper_coffee\\pic\\' + '00_1.png', roi)
- # cv2.imshow("ROI_WINDOW",roi)
- # cv2.waitKey(0)
- def roi2():
- x = r2[1]
- x1 = r2[2]
- y = r2[3]
- y1 = r2[4]
- i = 1
- number = 1
- # roi [y:y1,x:x1]
- # rectangle (x.y),(x1,y1)
- roi = img[y:y1, x:x1]
- cv2.rectangle(img, (x, y), (x1, y1), point_color, 1)
- roi = cv2.resize(roi,(image_size, image_size), 0, 0, cv2.INTER_LINEAR)
- cv2.imwrite('D:\\fatwolf\\company_files\\paper_coffee\\pic\\' + '00_2.png', roi)
- # cv2.imshow("ROI_WINDOW",roi)
- # cv2.waitKey(0)
- def roi3():
- x = r3[1]
- x1 = r3[2]
- y = r3[3]
- y1 = r3[4]
- i = 1
- # roi [y:y1,x:x1]
- # rectangle (x.y),(x1,y1)
- roi = img[y:y1, x:x1]
- cv2.rectangle(img, (x, y), (x1, y1), point_color, 1)
- roi = cv2.resize(roi,(image_size, image_size), 0, 0, cv2.INTER_LINEAR)
- cv2.imwrite('D:\\fatwolf\\company_files\\paper_coffee\\pic\\' + '00_3.png', roi)
- # cv2.imshow("ROI_WINDOW",roi)
- # cv2.waitKey(0)
- def roi4():
- x = r4[1]
- x1 = r4[2]
- y = r4[3]
- y1 = r4[4]
- i = 1
- # roi [y:y1,x:x1]
- # rectangle (x.y),(x1,y1)
- roi = img[y:y1, x:x1]
- cv2.rectangle(img, (x, y), (x1, y1), point_color, 1)
- roi = cv2.resize(roi,(image_size, image_size), 0, 0, cv2.INTER_LINEAR)
- cv2.imwrite('D:\\fatwolf\\company_files\\paper_coffee\\pic\\' + '00_4.png', roi)
- # cv2.imshow("ROI_WINDOW",roi)
- # cv2.waitKey(0)
- start = datetime.datetime.now()
- roi1()
- roi2()
- roi3()
- roi4()
- end = datetime.datetime.now()
- print("cut_rectangle Run Time:", end - start)
- # cv2.imshow('roi1', roi1)
- # cv2.imshow("image", img)
- def cnn():
- # data file
- data_dir = "D:\\fatwolf\\company_files\\paper_coffee\\pic\\"
- # train or test
- train = False
- # model address
- model_path = "model/image_model"
- def read_data(data_dir):
- datas = []
- labels = []
- fpaths = []
- for fname in os.listdir(data_dir):
- fpath = os.path.join(data_dir, fname)
- fpaths.append(fpath)
- image = Image.open(fpath)
- data = np.array(image) / 255.0
- label = str(fname.split("_")[0])
- datas.append(data)
- labels.append(label)
- datas = np.array(datas)
- labels = np.array(labels)
- #print("shape of datas: {}\tshape of labels: {}".format(datas.shape, labels.shape))
- return fpaths, datas, labels
- fpaths, datas, labels = read_data(data_dir)
- # num_classes = len(set(labels))
- num_classes = 4
- datas_placeholder = tf.compat.v1.placeholder(tf.float32, [None, 150, 150, 3])
- labels_placeholder = tf.placeholder(tf.int32, [None])
- dropout_placeholdr = tf.placeholder(tf.float32)
- conv0 = tf.layers.conv2d(datas_placeholder, 20, 5, activation=tf.nn.relu)
- pool0 = tf.layers.max_pooling2d(conv0, [2, 2], [2, 2])
- conv1 = tf.layers.conv2d(pool0, 40, 4, activation=tf.nn.relu)
- pool1 = tf.layers.max_pooling2d(conv1, [2, 2], [2, 2])
- flatten = tf.layers.flatten(pool1)
- fc = tf.layers.dense(flatten, 400, activation=tf.nn.relu)
- dropout_fc = tf.layers.dropout(fc, dropout_placeholdr)
- logits = tf.layers.dense(dropout_fc, num_classes)
- predicted_labels = tf.arg_max(logits, 1)
- losses = tf.nn.softmax_cross_entropy_with_logits(
- labels=tf.one_hot(labels_placeholder, num_classes),
- logits=logits
- )
- mean_loss = tf.reduce_mean(losses)
- optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=1e-2).minimize(losses)
- saver = tf.compat.v1.train.Saver()
- with tf.compat.v1.Session() as sess:
- if train:
- print("train mode")
- sess.run(tf.global_variables_initializer())
- train_feed_dict = {
- datas_placeholder: datas,
- labels_placeholder: labels,
- dropout_placeholdr: 0.25
- }
- for step in range(500):
- _, mean_loss_val = sess.run([optimizer, mean_loss], feed_dict=train_feed_dict)
- if step % 50 == 0:
- print("step = {}\tmean loss = {}".format(step, mean_loss_val))
- saver.save(sess, model_path)
- print("train done save model{}".format(model_path))
- else:
- #start = datetime.datetime.now()
- #print("reloading model")
- saver.restore(sess, model_path)
- #print("{}reload model".format(model_path))
- label_name_dict = {
- 0: "Brokenbeans",
- 1: "Peaberry",
- 2: "shellbean",
- 3: "Worms"
- }
- test_feed_dict = {
- datas_placeholder: datas,
- labels_placeholder: labels,
- dropout_placeholdr: 0
- }
- predicted_labels_val = sess.run(predicted_labels, feed_dict=test_feed_dict)
- for fpath, real_label, predicted_label in zip(fpaths, labels, predicted_labels_val):
- # real_label_name = label_name_dict[real_label]
- predicted_label_name = label_name_dict[predicted_label]
- print("{}\t => {}".format(fpath, predicted_label_name))
- #print(fpath, predicted_label_name)
- dirListing = os.listdir(data_dir)
- #print(len(dirListing))
- #end = datetime.datetime.now()
- #print("執行時間:", end - start)
- x = 1
- #if __name__ == '__main__':
- while True:
- start = datetime.datetime.now()
- #takephoto()
- cut_rectangle()
- cnn()
- '''
- evt = 'notify_me' # 事件名稱
- key = 'c3xo5EvpBX64fPEqxphcR4jBTzDh1r2joTDsB_BslOA'
- val1 = parse.quote('執行第') # value1參數值
- val2 = parse.quote(str(x)) # value2參數值
- val3 = parse.quote('次') # value3參數值
- url = (f'https://maker.ifttt.com/trigger/{evt}' +
- f'/with/key/{key}?value1={val1}&value2={val2}&value3={val3}')
- r = req.get(url) # 執行IFTTT平台的webhooks
- r.text # 取得IFTTT的回應
- x = x+1
- '''
- end = datetime.datetime.now()
- print("完整執行時間:", end - start)
- print('-----------------------------------------------------------')
- #time.sleep(1)
- tf.reset_default_graph()
|