def test_crop_per(): crop(scr_folder="images", dest_folder="cper", px=None, per=50) a_images = glob.glob("./images/*.*") o_images = glob.glob("./cper/*.*") im1 = Image.open(o_images[0]) im2 = Image.open(os.path.join("images", o_images[0].split("\\")[-1])) assert im1.size[0] < im2.size[0]
def main(): #initialize the crop matrix using the following syntax: crop_matrix = [[crop("NULL") for x in range(5)] for x in range(5)] seed_list = [] bank_balance = bank(250) numb_of_seeds = [0,0,0,0,0,0] seed_list.append(seed("Wheat")) seed_list.append(seed("Rice")) seed_list.append(seed("Cotton")) seed_list.append(seed("Jute")) seed_list.append(seed("Banana")) seed_list.append(seed("Apple")) start_time = time.time() print(start_time) print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) add_seed("Wheat",seed_list,numb_of_seeds,bank_balance) print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) add_seed("Cotton",seed_list,numb_of_seeds,bank_balance) print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) add_to_crop_matrix("Wheat",0,0,crop_matrix,seed_list,numb_of_seeds) crop_matrix[0][0].water() crop_matrix[0][0].water() crop_matrix[0][0].water() crop_matrix[0][0].water() crop_matrix[0][0].water() crop_matrix[0][0].water() print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) crop_matrix[0][0].water() print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) crop_matrix[0][0].water() print_crop_matrix(crop_matrix,bank_balance,seed_list,numb_of_seeds) print(time.time()-start_time)
x1, y1 = cell_b[1] b = cropped[(y0 - M):(y1 + M), (x0 - M):(x1 + M)] tb = cropped[(y0 + P):(y1 - P), (x0 + P):(x1 - P)] if np.mean(tb) > B: return 0 m = cv2.matchTemplate(a, tb, cv2.TM_CCOEFF_NORMED) n = cv2.matchTemplate(b, ta, cv2.TM_CCOEFF_NORMED) return (cv2.minMaxLoc(m)[1] + cv2.minMaxLoc(n)[1]) / 2 if __name__ == '__main__': a = sys.argv[1] n = a[:a.find('.')] original = cv2.imread(a) cropped = crop(original, m=30, f=6, fast=True) cells = cells(cropped) erased = np.copy(cropped) for column in cells: for cell in column[1:]: adjust_borders(erased, cell) erase_borders(erased, cell, borders_adjusted=True) highlighted = np.copy(cropped) for column in cells[1:-1]: scores = [False] * len(column) for i in range(2, len(column) - 2): for j in range(i + 1, len(column) - 1):
import numpy as np import cv2 import pygame # modules:: from crop import * from checkingGrid import * ## Image to calibrate where the grid is located image_calibration = cv2.imread('Pictures/gridBlob.jpg') ## Image to actually crop (should be the orignal one) image = cv2.imread('Pictures/test_2.jpg') crop(image_calibration, image, "Grid_Image.jpg") # PATH TO NEW IMAGE IS Pictures/Grid_Image.jpg !! for point in find_shapes('Pictures/Grid_Image.jpg'): print(point) for MinMax in shape_positions(image,point): print(MinMax) fill_array(MinMax,placementArr) #PRINTING THE ARRAY< FOR DEBUGGING for row in placementArr: print(row)
def detect(filename, folder, file_no): # print 'in' img = cv2.imread(filename) img = cv2.medianBlur(img, 1) #smothing image # extracting white characters image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgray = crop(image) # imgray = image imgray = cv2.resize(imgray, (880, 100)) (h, w) = imgray.shape (img_h, img_w) = (h, w) drawing = noise(imgray, imgray.shape, 1000, 25000) cv2.imshow('win1', drawing) cv2.waitKey() cv2.imwrite('E:\Results\\res.jpg', drawing) # character recorgnition image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) lang = ['eng'] result = 0 for i in lang: data = pytesser.iplimage_to_string(image, i, pytesser.PSM_SINGLE_LINE, makebox=True) # print data word = '' section = [] #all the characters and coordinates in a list line = [] for i in data: if i == ' ' or i == '\n': line.append(word) word = '' elif (i != '\n'): word += i if i == '\n': section.append(line) line = [] coord = [] chars = [] #coordinates of the characters count = 0 for i in section: for j in i: # print count if j.isdigit() and count < 5 and count > 0: coord.append(int(j)) count += 1 count = 0 chars.append(coord) coord = [] # removing all symbols dele = 0 delete = [] for i in section: if not i[0].isdigit() and not i[0].isalpha(): delete.append(dele) dele += 1 delete.reverse() for i in delete: section.pop(i) chars.pop(i) c = 0 point = [] for i in chars: cv2.rectangle(drawing, (i[0], i[1]), (i[2], i[3]), (255, 255, 255), 2) cv2.imshow('rec', drawing) cv2.waitKey(1) cv2.imwrite('E:\\report\\res.jpg', drawing) counter = 0 line_coordinates = [0] if len(chars) == 1: for i in chars: if i[0] > 400: line_coordinates.append(i[0] / 2) line_coordinates.append(i[0]) # for debugging, draws box using coordinates received from make box function for i in chars: counter += 1 if c == 0: point.append(i[2]) c = 1 elif c == 1: point.append(i[0]) line = ((point[0] + point[1]) / 2) line_coordinates.append(line) cv2.line(drawing, (line, img_h), (line, 0), (255, 255, 255), 2) c = 0 point = [] if counter == len(chars): if img_w - i[2] < 150: line_coordinates.append(img_w) else: line_coordinates.append(i[2]) line_coordinates.append(img_w) cv2.line(drawing, (i[2], img_h), (i[2], 0), (255, 255, 255), 2) # spliting image and processing iterator = 1 id = '' while iterator < len(line_coordinates): roi = imgray[0:img_h, line_coordinates[iterator - 1]:line_coordinates[iterator]] iterator += 1 corrected = noise(roi, roi.shape, 300, 20000) cv2.imwrite('E:\Results\\res.jpg', corrected) cv2.imshow('win1', corrected) cv2.waitKey(1) image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) data = pytesser.iplimage_to_string(image, 'enm', 7) # print data for i in data: if i == '!': i = '1' if i.isalpha() or i.isdigit(): if i == 'O' or i == 'o': i = '0' if i == 'L' or i == 'l': i = '1' if i == 'i' or i == 'I': i = '1' id = id + i # print id if id != '': print id sol = check(id) final = sol[0] alternative = sol[1] possible = sol[2] else: return None # print sol if not final: os.chdir(folder) filename = 'error' + str(file_no) img_name = filename + '.jpg' txt_name = filename + '.txt' cv2.imwrite(img_name, img) if len(alternative) > 1: #storing alternatives in a text file f = open(txt_name, 'w') for i in alternative: f.write(i + '\n') f.close() return None else: return possible
def remove_from_crop_matrix(i,j,crop_matrix): crop_matrix[i][j]=crop("null")
def add_to_crop_matrix(crop_name,i,j,crop_matrix,seed_list,numb_of_seeds): if(check_for_empty_cell_crop_matrix(crop_matrix,i,j)==True): if(delete_seed(crop_name,seed_list,numb_of_seeds)==True): crop_matrix[i][j] = crop(crop_name)
def detect(filename, folder, file_no): # print 'in' img = cv2.imread(filename) img = cv2.medianBlur(img, 1) #smothing image # extracting white characters image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgray = crop(image) # imgray = image imgray = cv2.resize(imgray, (880,100)) (h,w) = imgray.shape (img_h,img_w) = (h,w) drawing = noise(imgray, imgray.shape, 1000, 25000) cv2.imshow('win1', drawing) cv2.waitKey() cv2.imwrite('E:\Results\\res.jpg', drawing) # character recorgnition image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) lang = ['eng'] result = 0 for i in lang: data = pytesser.iplimage_to_string(image, i, pytesser.PSM_SINGLE_LINE,makebox=True) # print data word = '' section = [] #all the characters and coordinates in a list line = [] for i in data: if i == ' ' or i=='\n': line.append(word) word = '' elif (i!='\n'): word+=i if i == '\n': section.append(line) line = [] coord = [] chars = [] #coordinates of the characters count = 0 for i in section: for j in i: # print count if j.isdigit() and count < 5 and count > 0: coord.append(int(j)) count += 1 count = 0 chars.append(coord) coord = [] # removing all symbols dele= 0 delete=[] for i in section: if not i[0].isdigit() and not i[0].isalpha(): delete.append(dele) dele += 1 delete.reverse() for i in delete: section.pop(i) chars.pop(i) c = 0 point = [] for i in chars: cv2.rectangle(drawing,(i[0],i[1]),(i[2],i[3]),(255,255,255),2) cv2.imshow('rec', drawing) cv2.waitKey(1) cv2.imwrite('E:\\report\\res.jpg', drawing) counter = 0 line_coordinates = [0] if len(chars) == 1: for i in chars: if i[0] > 400: line_coordinates.append(i[0]/2) line_coordinates.append(i[0]) # for debugging, draws box using coordinates received from make box function for i in chars: counter += 1 if c == 0: point.append(i[2]) c = 1 elif c == 1: point.append(i[0]) line = ((point[0] + point[1]) / 2) line_coordinates.append(line) cv2.line(drawing,(line,img_h),(line,0),(255,255,255),2) c = 0 point = [] if counter == len(chars): if img_w - i[2] < 150: line_coordinates.append(img_w) else: line_coordinates.append(i[2]) line_coordinates.append(img_w) cv2.line(drawing,(i[2],img_h),(i[2],0),(255,255,255),2) # spliting image and processing iterator = 1 id = '' while iterator < len(line_coordinates): roi = imgray[0:img_h, line_coordinates[iterator-1]:line_coordinates[iterator]] iterator += 1 corrected = noise(roi, roi.shape, 300, 20000) cv2.imwrite('E:\Results\\res.jpg', corrected) cv2.imshow('win1', corrected) cv2.waitKey(1) image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) data = pytesser.iplimage_to_string(image, 'enm', 7 ) # print data for i in data: if i == '!': i = '1' if i.isalpha() or i.isdigit(): if i == 'O' or i =='o': i = '0' if i == 'L' or i == 'l': i = '1' if i == 'i' or i == 'I': i = '1' id = id + i # print id if id != '': print id sol = check(id) final = sol[0] alternative = sol[1] possible = sol[2] else: return None # print sol if not final: os.chdir(folder) filename = 'error' + str(file_no) img_name = filename + '.jpg' txt_name = filename + '.txt' cv2.imwrite(img_name, img) if len(alternative) > 1: #storing alternatives in a text file f = open(txt_name, 'w') for i in alternative: f.write(i+'\n') f.close() return None else: return possible