ekelner/menuAnalysis.py

58 lines
1.6 KiB
Python

import numpy as np
import argparse
import cv2
from photoAnalysisUtils import *
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]
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()
image, x, y = searchReferencePointCorner(image)
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")
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)
return result_vector
# img_tested = cv2.imread('orders/1.jpg')
# menuAnalyseIMG(img_tested)