###Final Stage### ### result="" size=630 size=(size,)*2 number=size[0]/9 print("OCR Stage") import cv2.cv as cv import OCR for i,j in enumerate(b): ri = i/10 ci = i%10 if ci != 9 and ri!=9: src = bm[ri:ri+2, ci:ci+2 , :].reshape((4,2)) dst = np.array( [ [ci*number,ri*number],[(ci+1)*number-1,ri*number],[ci*number,(ri+1)*number-1],[(ci+1)*number-1,(ri+1)*number-1] ], np.float32) retval = cv2.getPerspectiveTransform(src,dst) warp = cv2.warpPerspective(res2,retval,(size)) whole_digit= warp[ri*number:(ri+1)*number-1 , ci*number:(ci+1)*number-1].copy() noborder_digit= whole_digit[5:64,5:64] gray_digit = cv2.cvtColor(noborder_digit,cv2.COLOR_BGR2GRAY) thresh_digit = cv2.adaptiveThreshold(gray_digit,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,0,151,80) image = cv.CreateImageHeader((thresh_digit.shape[1],thresh_digit.shape[0]), cv.IPL_DEPTH_8U, 1) cv.SetData(image, thresh_digit.tostring(), thresh_digit.dtype.itemsize*thresh_digit.shape[1]) result+=OCR.ocr_singledigit(image) rows = OCR.split_len(result,9) for row in rows: print row print time.time()-st
for row in range(9): print "\tRow: " + str(row+1) for column in range(9): ## top left corner = x1:y1 | down right corner = x2:y2 x1,x2,y1,y2 = OCR.getcorners((row,column),r_points) crop = res2[y1+5 : y2-5 , x1+5 : x2-5] if crop.size == 0: result[row][column] = "E" continue cv2.imwrite("debug/digits"+str(row)+str(column)+".jpg",crop) digit = cv2.cv.CreateImageHeader((crop.shape[1],crop.shape[0]), cv2.cv.IPL_DEPTH_8U, 1) cv2.cv.SetData(digit, crop.tostring(), crop.dtype.itemsize*crop.shape[1]) ##recognize the digit result[column][row] = OCR.ocr_singledigit(digit) ##print out the sudoku puzzle for row in result: string = "" for digit in row: string+= str(digit) + " " print string print print "Total time: " + str(time.time()-timer_start)
for row in range(9): print "Row:", row for column in range(9): src = bm[row:row+2, column:column+2 , :].reshape((4,2)) dst = np.array( [ [column*sqaure_size,row*sqaure_size],[(column+1)*sqaure_size-1,row*sqaure_size],[column*sqaure_size,(row+1)*sqaure_size-1],[(column+1)*sqaure_size-1,(row+1)*sqaure_size-1] ], np.float32) retval = cv2.getPerspectiveTransform(src,dst) warp = cv2.warpPerspective(res2,retval,(size)) whole_digit = warp[row*sqaure_size:(row+1)*sqaure_size-1 , column*sqaure_size:(column+1)*sqaure_size-1].copy() noborder_digit = whole_digit[5:64,5:64] gray_digit = cv2.cvtColor(noborder_digit,cv2.COLOR_BGR2GRAY) thresh_digit = cv2.adaptiveThreshold(gray_digit,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,0,151,80) image = cv2.cv.CreateImageHeader((thresh_digit.shape[1],thresh_digit.shape[0]), cv2.cv.IPL_DEPTH_8U, 1) cv2.cv.SetData(image, thresh_digit.tostring(), thresh_digit.dtype.itemsize*thresh_digit.shape[1]) result[row][column] = OCR.ocr_singledigit(image) for row in row: row_string = "" for digit in row: string += digit + " " print string print print "Total time: " + str(time.time()-timer_start)
print "\t[+]Row: " + str(row + 1) for column in range(9): ## top left corner = x1:y1 | down right corner = x2:y2 x1, x2, y1, y2 = OCR.getcorners((row, column), R_POINTS) crop = RES2[y1 + 7 : y2 - 7 , x1 + 7: x2 - 7] digit = cv2.cv.CreateImageHeader((crop.shape[1], crop.shape[0]), cv2.cv.IPL_DEPTH_8U, 1) cv2.cv.SetData(digit, crop.tostring(), crop.dtype.itemsize*crop.shape[1]) if DEBUG_MODE: print row, column cv2.imwrite(DEBUG_FOLDER + str(row) + str(column)+".jpg", crop) ##recognize the digit RESULT[column][row] = OCR.ocr_singledigit(digit) ##print out the sudoku puzzle print print "[+]Solution:" for row in RESULT: string = "" for digit in row: string += str(digit) + " " print string print print "Total time: " + str(round(time.time() - TIMER_START, 2)) + " seconds"