diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0739ae0..83c77e6 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,14 @@ - + + + + + + + + + + true DEFINITION_ORDER - - @@ -37,9 +41,15 @@ - + + - + + + + + + + + + + + + + + + + + + + + + @@ -137,53 +219,57 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + + + + + \ No newline at end of file diff --git a/directoryListener.py b/directoryListener.py new file mode 100644 index 0000000..18e1b2b --- /dev/null +++ b/directoryListener.py @@ -0,0 +1,82 @@ +from datetime import datetime +from os import scandir +from time import sleep +import numpy as np +import argparse +import cv2 +from photoAnalysisUtils import * +from menuAnalysis import * +from mysqlConnect import * + + +def convert_date(timestamp): + d = datetime.utcfromtimestamp(timestamp) + formated_date = d.strftime('%d %b %Y %H:%M:%S"') + return formated_date + +def get_files(): + dir_entries = scandir('../orders/') + for entry in dir_entries: + if entry.is_file(): + info = entry.stat() + #print(f'{entry.name}\t Created: {convert_date(info.st_ctime)}') + +def compareNewest(newest_info): + dir_entries = scandir('orders/') + for entry in dir_entries: + if entry.is_file(): + info = entry.stat() + info = info.st_ctime + if datetime.fromtimestamp(info) > datetime.fromtimestamp(newest_info): + return entry + return True + + + + + +newest_file = 0 +newest_info = 0 +dir_entries = scandir('orders/') +for entry in dir_entries: + if entry.is_file(): + info = entry.stat() + info = info.st_ctime + if newest_file == 0: + newest_file = entry + newest_info = newest_file.stat() + newest_info = newest_info.st_ctime + # print(convert_date(info.st_ctime)) + #print(f'{newest_file.name}\t Przypisany: {convert_date(newest_info)}') + else: + if datetime.fromtimestamp(info) > datetime.fromtimestamp(newest_info): + #print(f'{entry.name}\t Nowszy: {convert_date(info)}') + #print(f'{newest_file.name}\t Starszy: {convert_date(newest_info)}') + newest_file = entry + newest_info = newest_file.stat() + newest_info = newest_info.st_ctime + + +try: + while True: + sleep(1) + if compareNewest(newest_info) != True: + # Sprawdzanie czy nowy plik + newest_file = compareNewest(newest_info) + newest_info = newest_file.stat() + newest_info = newest_info.st_ctime + print('Nowy plik to:') + print(datetime.fromtimestamp(newest_info)) + print(f'{newest_file.name}\t Data: {newest_info}') + print(newest_file.name) + + # URL Do analizy pliku + url = 'orders/'+ str(newest_file.name) + print(url) + img_tested = cv2.imread(url) + mySqlRecords(menuAnalyseIMG(img_tested)) + + + +except KeyboardInterrupt: + pass \ No newline at end of file diff --git a/menuAnalysis.py b/menuAnalysis.py index 905639d..612f7c7 100644 --- a/menuAnalysis.py +++ b/menuAnalysis.py @@ -3,49 +3,56 @@ import argparse import cv2 from photoAnalysisUtils import * -img_tested = cv2.imread("samples/CCI18022020.jpg") -image = img_tested +def menuAnalyseIMG(url): + result_vector = [] + img_tested = url + image = img_tested -blurred = cv2.GaussianBlur(image, (5, 5), 0) -edged = cv2.Canny(blurred, 20, 50) -height = image.shape[0] -width = image.shape[1] + blurred = cv2.GaussianBlur(image, (5, 5), 0) + edged = cv2.Canny(blurred, 20, 50) + height = image.shape[0] + width = image.shape[1] -referencePoint = open("referencePoint.txt", "r") -checkBoxesCords = open("checkBoxesCords.txt", "r") -referencePoint = referencePoint.readlines() -checkBoxesCords = checkBoxesCords.readlines() + referencePoint = open("referencePoint.txt", "r") + checkBoxesCords = open("checkBoxesCords.txt", "r") + referencePoint = referencePoint.readlines() + checkBoxesCords = checkBoxesCords.readlines() -for corners in referencePoint: - template_x, template_y = corners.split() + for corners in referencePoint: + template_x, template_y = corners.split() -image, x, y = searchReferencePointCorner(image) + image, x, y = searchReferencePointCorner(image) -offsetX = int(template_x) - x -offsetY = int(template_y) - y -i=1 + offsetX = int(template_x) - x + offsetY = int(template_y) - y + i=1 -for checkBox in checkBoxesCords: - x, y, w, h = checkBox.split() - x = int(x) - offsetX - y = int(y) - offsetY - w = int(w) - h = int(h) - cv2.rectangle(image, (x, y), (x + w, y + h), (36, 255, 12), 2) - crop_img = edged[y:y + h, x:x + w] - n_white_pix = np.sum(crop_img == 255) - n_black_pix = np.sum(crop_img == 0) - if(n_white_pix+n_black_pix)*0.2 < n_white_pix: - print(str(i)+" FILL") - else: - print(str(i)+" EMPTY") - #print("White i ="+str(i)+" white = "+str(n_white_pix)) - #print("Black i ="+str(i)+" black = "+str(n_black_pix)) - #cv2.imwrite("maska"+str(i)+".jpg", crop_img) - i = i+1 + for checkBox in checkBoxesCords: + x, y, w, h = checkBox.split() + x = int(x) - offsetX + y = int(y) - offsetY + w = int(w) + h = int(h) + cv2.rectangle(image, (x, y), (x + w, y + h), (36, 255, 12), 2) + crop_img = edged[y:y + h, x:x + w] + n_white_pix = np.sum(crop_img == 255) + n_black_pix = np.sum(crop_img == 0) + if(n_white_pix+n_black_pix)*0.2 < n_white_pix: + #print(str(i)+" FILL") + result_vector.append([i, 1]) + else: + #print(str(i)+" EMPTY") + result_vector.append([i, 0]) + #print("White i ="+str(i)+" white = "+str(n_white_pix)) + #print("Black i ="+str(i)+" black = "+str(n_black_pix)) + #cv2.imwrite("maska"+str(i)+".jpg", crop_img) + i = i+1 -cv2.imwrite("tested.jpg", image) + cv2.imwrite("tested.jpg", image) + return result_vector +# img_tested = cv2.imread('orders/1.jpg') +# menuAnalyseIMG(img_tested) \ No newline at end of file diff --git a/mysqlConnect.py b/mysqlConnect.py new file mode 100644 index 0000000..628b6d9 --- /dev/null +++ b/mysqlConnect.py @@ -0,0 +1,70 @@ +import mysql.connector +import random +import string + +from datetime import datetime + +def id_generator(size=5, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for _ in range(size)) + + +def mySqlRecords(array): + mydb = mysql.connector.connect( + host="localhost", + user="root", + password="", + database="kelner" + ) + + timestamp = 1545730073 + mycursor = mydb.cursor(buffered=True) + # Wyciąganie aktualnego aktywnego szablonu oraz przypisywanie ID do activeTemplateID + mycursor.execute("SELECT id FROM template WHERE active=1") + myresult = mycursor.fetchone() + activeTemplateID = str(myresult[0]) + + + + # for x in myresult: + # print(x) + + + price = 0 + # array = [[1, 0], [2, 0], [3, 1], [4, 0], [5, 0], [6, 0], [7, 1], [8, 0], [9, 1]] + + # Wyliczanie ceny + for x in array: + if x[1] == 1: + # Wyciaganie szablonu dan po id szablonu oraz numerze checkboxu + checkboxnumber = str(x[0]) + mycursor.execute("SELECT * FROM templatedishmap WHERE template_id=" + activeTemplateID + " AND checkboxnumber=" + checkboxnumber) + myresult = mycursor.fetchone() + price = price + myresult[3]*myresult[5] + + # Dodanie zamówienia + sql = "INSERT INTO orders (number, price, active, created_at) VALUES (%s, %s, 1, %s)" + val = (str(id_generator()), str(price), str(datetime.fromtimestamp(timestamp))) + mycursor.execute(sql, val) + mydb.commit() + + # Sprawdzenie id ostatniego zamowienia + mycursor = mydb.cursor(buffered=True) + mycursor.execute("SELECT id FROM orders ORDER BY id DESC") + myresult = mycursor.fetchone() + addedID = myresult[0] + + + + for x in array: + if x[1] == 1: + # Wyciaganie szablonu dan po id szablonu oraz numerze checkboxu + checkboxnumber = str(x[0]) + mycursor.execute("SELECT * FROM templatedishmap WHERE template_id=" + activeTemplateID + " AND checkboxnumber=" + checkboxnumber) + myresult = mycursor.fetchone() + templateDishMapID = myresult[0] + # Dodawanie poszczegolnych potraw do zamowienia + sql = "INSERT INTO orders_template_dish_map (order_id, template_id, created_at) VALUES (%s, %s, %s)" + val = (str(addedID), str(templateDishMapID), str(datetime.fromtimestamp(timestamp))) + mycursor.execute(sql, val) + + mydb.commit() diff --git a/orders/CCI18022020.jpg b/orders/CCI18022020.jpg deleted file mode 100644 index f15b47f..0000000 Binary files a/orders/CCI18022020.jpg and /dev/null differ diff --git a/photoAnalysisUtils.py b/photoAnalysisUtils.py index 0d9885e..0884785 100644 --- a/photoAnalysisUtils.py +++ b/photoAnalysisUtils.py @@ -22,6 +22,7 @@ def searchReferencePointCorner(image): x, y, w, h = cv2.boundingRect(approx) aspect_ratio = w / float(h) area = cv2.contourArea(c) + if len(approx) == 4 and area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1): # Prawy górny róg if x > width * 0.80 and y < height * 0.20: