def getSimplifiedFromImage(): ''' Take image in the form of a JSON object, returns the simplfied text ''' result = request.get_json() requires = ["img"] if isValid(requires, result): info = result["img"] if info[1:21] == "data:application/pdf": type = "PDF" else: type = "IMG" base64Image = info[info.find(',') + 1:] text = 'undefined' if type == "PDF": text = OCR.process_PDF(base64Image) elif type == "IMG": text = OCR.process_IMG(base64Image) return text else: logging.info("Invalid request.") return "Failed to process image" return "OK"
def check(): if "blank" in OCR.resolve_url(): print("! RESET") return this_func(selected_link) else: if OCR.check_current_url(): time.sleep(1) cmd = Commands() cmd.go_to_top()
def main(): print("Loading Images ...") # KNN ML Model ################################################################################################################ blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN( CLASS_TXT_DIR, FLAT_IMG_DIR) # attempt KNN training if blnKNNTrainingSuccessful == False: print("\nerror: KNN traning was not successful\n") return # end if # Loading Images ############################################################################################################## BR_IMGS = os.listdir(DATASET_DIR) BR_IMGS = [img for img in BR_IMGS if ".jpg" in img] extractedPlates = [] numberPlateList = [] filename = [] # Write extracted plates images to path print("Begin Pre-processing images ...") for BR_IMG in BR_IMGS: BR_IMG_path = DATASET_DIR + BR_IMG imgOriginal = cv2.imread(BR_IMG_path) listOfPossiblePlates = DetectPlates.detectPlates(imgOriginal) listOfPossiblePlates = DetectChars.detectCharsInPlates( listOfPossiblePlates) # Sorting list of possible plates in DESC order (the one with most chars recognized) listOfPossiblePlates.sort( key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) licPlate = listOfPossiblePlates[0] extractedPlates.append(licPlate.imgPlate) numberPlateList.append(BR_IMG.rsplit('.', 1)[0]) cv2.imwrite(EXTRACTED_PLATES_DIR + BR_IMG, licPlate.imgPlate) # Export list of number plate to csv WriteNumberPlateList(numberPlateList) print("Completed pre-processing and export list of number plates.") # Pre-process extracted plates and export processed image file print("Begin Image Pre-processing for OCR..") ocr_preprocessed_plates = OCR.preprocess(extractedPlates, numberPlateList, PREPROCESSED_PLATES_DIR) # OCR the preprocessed extracted plates and export the OCR Output to csv OCROutput = OCR.OCR(ocr_preprocessed_plates) print("Completed Image Pre-processing for OCR and export to csv.") # Accuracy result based on predicted number plate vs actual number plates OCR_Result = OCR.CheckOCR(OCROutput, numberPlateList) # Export Result to csv ExportActualAndPredictedNumberPlate(numberPlateList, OCROutput) return # end of main()
def setUp(self): base_dir = os.path.abspath(os.getcwd()) with open('det_args.json') as f: self.det_args = json.load(f) path = os.environ.get("DET_MODEL_PATH") self.det_args["trained_model"] = os.path.join(base_dir, path) self.det_net = ocr.init_detection_model(self.det_args) with open('rec_args.json') as f: self.rec_args = json.load(f) path = os.environ.get("REC_MODEL_PATH") self.rec_args["trained_model"] = os.path.join(base_dir, path) self.rec_net = ocr.init_rec_model(self.rec_args)
def index(): if request.method == 'POST': image_file = request.files['file'] if image_file: filepath = os.path.join('uploads', image_file.filename) image_file.save(filepath) language = detect_language(filepath) result_file_path = 'recognized/' + image_file.filename + '.txt' OCR.recognizeFile(filepath=filepath, resultFilePath=result_file_path, language=language, outputFormat='txt') return translate(filepath=result_file_path), 500 return render_template('main/index.html')
def toPDF(): result = request.get_json() requires = ["img"] if isValid(requires, result): info = result["img"] if info[1:21] == "data:application/pdf": type = "PDF" else: type = "IMG" if type == "PDF": return OCR.PDFFromBase64(info[info.find(',') + 1:]) elif type == "IMG": return OCR.PDFFromBase64(info[info.find(',') + 1:])
def payment_solver(self, selected_link, sensitive=False): self.wait_for("Basarılı mı?", f'assets/{selected_link}/success', scrolldown_enabled=True, sensitive=sensitive) offset = self.config["payment_solver"][selected_link]["region_offset"] pos = self.find(f'assets/{selected_link}/resolve_payment', sensitive=sensitive) if pos: pos = list(pos) pos[0] += offset[0] pos[1] += offset[1] pos[2] = offset[2] pos[3] = offset[3] save_path = f"payments/{selected_link}" if not os.path.isdir(save_path): os.makedirs(save_path, exist_ok=True) payment_solver_text = OCR.resolve_region( pos, show=True, save= f"{save_path}/{str(datetime.datetime.now()).split('.')[0].replace(':','-')}.png" ) print("Payment Image Pos:", pos) payment_amount = re.match("\d+", payment_solver_text) if payment_amount: with open(f"payments/{selected_link}.txt", 'a+') as f: str_log = f"#Time: {str(datetime.datetime.now()).split('.')[0]}, #Payment: {payment_amount[0]}" print(str_log) f.write(str_log + "\n") else: print("Could not detect payment solver image")
def main(): #Clone testimages from cloud storage subprocess.call("gsutil cp -r -n gs://smartreviewdata/testimages .", shell=True) # Run Object Detection and create image parts print("\nObject Detection Running and Exporting into imageparts...") OD.predict_boxes() #Push xmls to cloud storage print("\nPushing XMLs to cloud storage ...") subprocess.call("gsutil mv testxmls/* gs://smartreviewdata/testxmls/", shell=True) # Calculate Rows data using OCR print("\nRows being calculated for the imageparts...") sno = database.query( 'SELECT count(*) from `demo.{}`'.format(table_id))[0][0] ROWS = OCR.calculate_data(sno, 'imageparts', train_flag='False', source='testimages') print("\nMatching with BIGQUERY records and pushing...") match_query(ROWS) #Push imageparts to cloud storage print("\nPushing imageparts to global_imageparts ...") subprocess.call( "gsutil mv imageparts/* gs://smartreviewdata/global_imageparts/", shell=True) #Clear testimages subprocess.call("rm -r testimages", shell=True)
def upload_file(): # check if the post request has the file part if 'file' not in request.files: resp = jsonify({'message': 'No file part in the request'}) resp.status_code = 400 return resp file = request.files['file'] if file.filename == '': resp = jsonify({'message': 'No file selected for uploading'}) resp.status_code = 400 return resp if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) resp = jsonify({'message': 'File successfully uploaded'}) dpi = 300 documentText, fname = OCR.Convert(file_path, dpi, str(1)) data = Extract.Info(fname) data = Align.restructure(data) return str(data) else: resp = jsonify({'message': 'Allowed file type is pdf'}) resp.status_code = 400 return resp
def get(self): url = request.args.get('url') token = request.args.get('token') state = request.args.get('state') url = url + '&token=' + token url_response = urllib.request.urlopen(url) img_array = np.array(bytearray(url_response.read()), dtype=np.uint8) plateImg = cv2.imdecode(img_array, -1) height, width = plateImg.shape[:2] warped = four_point_transform( plateImg, np.array([(0, 0), (width, 0), (width, height), (0, height)])) h, w = warped.shape[:2] ratio = w / h height = int(300 / ratio) #imgPlate = cv2.resize(warped, (300, height)) #cv2.imshow('lol',imgPlate) #cv2.waitKey(0) read1, read2 = OCR.readRC(warped) dic = parseRC.parseToJSON(read1, read2, state) print(dic) return jsonify(dic)
def img_convert_str(): global result, img_path appid_str = appid.get() apikey_str = apikey.get() secretkey_str = secretkey.get() result = OCR.img_to_str(img_path, appid_str, apikey_str, secretkey_str) result_text.insert(INSERT, result)
def get(self): url = request.args.get('url') token = request.args.get('token') correctorOn = True CNN_OCR = True url = url + '&token=' + token url_response = urllib.request.urlopen(url) img_array = np.array(bytearray(url_response.read()), dtype=np.uint8) plateImg = cv2.imdecode(img_array, -1) height, width = plateImg.shape[:2] warped = four_point_transform( plateImg, np.array([(0, 0), (width, 0), (width, height), (0, height)])) validChars = Segmentation.startSegment(warped) print(len(validChars)) plateText = OCR.readPlate(validChars, correctorOn, CNN_OCR) print(plateText) if plateText != 'ignore': return jsonify(plate=plateText) else: return jsonify(plate='Try Again!')
def swipe(start, end, t): global count cmd = "adb shell input swipe 500 %d 500 %d 2000"%(start, end) subprocess.Popen(str(cmd), shell=True) time.sleep(3) for i in range(6): click(600, 445 + i * 270, 2+random.random()) click(540, 550, 4+random.random()) count = count + 1 screenshot(r"./picture/", count) time.sleep(2+random.random()) OCR.ocr_function(("picture/%d.png"%(count)), count) back() time.sleep(1+random.random()) back() time.sleep(1+random.random()) time.sleep(1+random.random())
def rowdownmid(team): time.sleep(5) T = time.clock() # assume loaded # click in screen BTcoords = None if (team == "blue"): while BTcoords is None: BTcoords = openCVLocate.locateCenter(imgpath + '/blueTeam.PNG') else: BTcoords = None clickInGame(BTcoords) # press y to lock camera keyHelper.PandRKey(keyHelper.cToHex('y')) # buy dorans blade screenshotArray = np.array(ImageGrab.grab().convert('RGB')) buyUpgradeAndBack.buy(buyorder, screenshotArray, BTcoords) cloop = 0 lastHealth = 0 time.sleep(40) # main loop # move back on big damage # remove slow move # make the loop[ faster for back and forth # buy on back # back on low health # level all abilities # use all abilities in a forward direction while True: cloop += 1 ctime = time.clock() screenshotArray = np.array(ImageGrab.grab().convert('RGB')) try: health = int(OCR.getWhiteText(screenshotArray, math.floor(BTcoords[0] - 545), 26, math.floor(BTcoords[1] + 120), 12, lower=150).replace('.', '').replace(' ', '')) if (health == 0): dead(screenshotArray, BTcoords) continue elif health < 200: keyHelper.PandRKey(keyHelper.cToHex('f')) buyUpgradeAndBack.back(buyorder, screenshotArray, BTcoords) continue if(health - lastHealth < -130): moveBackwards(1, BTcoords) autoDownMid(BTcoords, time.clock() - T) if(openCVLocate.locateCenter(imgpath + '/upgrade.PNG') is not None): buyUpgradeAndBack.upgrade() if cloop % 10 == 0: autoDownMid(BTcoords, time.clock() - T) print(health) lastHealth = health except ValueError: print("health got messed up") finally: time.sleep(max(.7 - (time.clock() - ctime), 0))
def welcome(): uid = uuid.uuid4() f =request.files['file'] file_name = uid.hex+f.filename f.save(secure_filename(file_name)) img = cv2.imread(file_name,0) os.remove(file_name) return OCR.detect(img)
def test_recognition_1(self): img_name = 'paper-title.jpg' img = loadImage(img_name) ocr.text_detection_results = ocr.text_detect(img, self.det_net, self.det_args) # check that 23 boxes are found in this input image self.assertEqual(len(ocr.text_detection_results['bboxes']), 23) # get crops from bounding boxes and input image bboxes = ocr.text_detection_results['bboxes'] crops = {} for k, v in bboxes.items(): bbox_coords = v.split(',') min_x = (int)(bbox_coords[0]) min_y = (int)(bbox_coords[1]) max_x = (int)(bbox_coords[4]) max_y = (int)(bbox_coords[5]) crops[k] = img[min_y:max_y, min_x:max_x, :] recognition_results = ocr.recognize(crops, self.rec_args, self.rec_net) self.assertEqual(len(recognition_results), 23) self.assertEqual(recognition_results['crop0']['result'], 'endtoend')
def main(): plate_no = OCR.predict() #plate_no = OcrCamera.predict() print("License plate is :{}".format(plate_no)) result = (searchnp.mysql_searchdata(plate_no)) if result == 1: ardino.main() print("gates open") else: print("gates will not open")
def crop(boxes, image): img = image x, y, w, h = boxes[ 0] #get the coordinates of the part the portion where the number plate is found crop_img = img[y:y + h, x:x + w] #cropping the image # cv2.imwrite('crop.jpg',crop_img) x = OCR.get_number( crop_img ) #sending the image for extraction of number using OCR from OCR space api return x
def main(): # Run Object Detection and create image parts print("Object Detection Running and Exporting into imageparts...") OD.predict_boxes() # Calculate Rows data using OCR print("Rows being calculated for the imageparts...") ROWS = OCR.calculate_data(36, 'imageparts') print("Matching with BIGQUERY records and pushing...") match_query(ROWS)
def chooseLesson(lesson): conn = httplib.HTTPConnection(host) # get captcha conn.request("GET", "/xk/input.jsp", None, headers) res = conn.getresponse() parser = MyHTMLParser() parser.feed(res.read()) token = parser.get_token() if token is None: print "have bugs in my programme" return False conn.request("GET", "/xk/image.do?token={}".format(token), None, headers) res = conn.getresponse() if res.getheader("Set-Cookie") is not None: headers["Cookie"] = res.getheader("Set-Cookie").split(";")[0] with open("captcha.jpg", "wb") as F: F.write(res.read()) # OCR OCRHdl = OCR.CaptchaHandler() rand = OCRHdl.captcha_from_image("captcha.jpg") # POST data = urllib.urlencode({"token": token, "selectionId": lesson, "xklb": "ss", "rand": rand}) try: time.sleep(3) conn.request("POST", "/xk/doSelectServlet", data, headers) res = conn.getresponse() F = open("out.html", "w") F.write(res.read()) F.close() print u"返回结果写在out.html中,请查看" except Exception as e: print u"请求失败" return False # check if lesson is selected conn.request("GET", "/xk/courseTableServlet", None, headers) res = conn.getresponse() parser.set_lesson(lesson) parser.feed(res.read()) conn.close() return parser.has_lesson()
def sendKeyword(self): inputwords = self.leKeyword.text() self.pbOCR.setValue(self.__i) # to ocrProgress self.stackedWidget.setCurrentIndex(6) QApplication.processEvents() self.__Ocr = OCR.CoverCheck(self.__DC.IMG_path, self.__DC.Crop_path, self.__DC.Improvement_path, inputwords) self.__Ocr.setOnstepListener(self) crop_list = self.__Ocr.crop(self.__coverList) improvement_list = self.__Ocr.improve(crop_list) detail_list = self.__Ocr.comparison(improvement_list) self.__DC.CROP_clear() self.__DC.IMPROVEMENT_clear() self.detailResult(detail_list) self.stackedWidget.setCurrentIndex(7)
def click_l(self, count=1, close_after_click=True): ''' if new tab is opened return [True] or return [False] ''' self.mouse.click(Mouse.Button.left, count) time.sleep(2) if not close_after_click: return False # Check URL if OCR.check_current_url(): time.sleep(1) self.go_to_top() return True return False
async def image_translate(ctx, lang): # получение отправителя команды author = ctx.message.author if lang.lower() == "ангглийский": await ctx.send( f"{author.mention}, зачем переводить на один и тот же язык?") return # обработка ошибок try: # доставание url из вложения url = str(ctx.message.attachments[0]) url = url[url.find("url") + 5:url.rfind("'")] # если ошибка except: # отправка сообщения ботом await ctx.send(f"{author.mention}, извините, я не могу найти картинку") # если нет ошибок else: # запрос к функции перевода изображения в текст text = OCR.image_to_text(url) if not text: await ctx.send( f"{author.mention}, извините, я не смог найти текста на английском на картинке" ) else: print(f"{author} использует команду image_translate", datetime.datetime.now()) # отправка текста в функции перевода с автоопределением языка await auto_translate(ctx, lang, text)
def refresh_coordinates_thread(): time.sleep(2) Upload.new_ship(0, 0, 0, settings.Variables.layerName, settings.Variables.layerID) nextGetTime = 0 while True: if time.time() > nextGetTime: nextGetTime = time.time() + 0.2 threading.Thread(target=OCR.refresh_coordinates()).start() if OCR.Coordinates.success: OCR.Coordinates.success = False # noinspection SpellCheckingInspection threading.Thread(target=Upload.move_ship, args=(OCR.Coordinates.x, OCR.Coordinates.y, OCR.Coordinates.z, settings.Variables.layerName, settings.Variables.layerID)).start() coordinates_display = "" coordinates_display = coordinates_display + "X: " + str( Upload.Vars.latestOnlineX) for i in range(0, 10 - len(str(Upload.Vars.latestOnlineX))): coordinates_display = coordinates_display + " " coordinates_display = coordinates_display + "\n" coordinates_display = coordinates_display + "Y: " + str( Upload.Vars.latestOnlineY) for i in range(0, 10 - len(str(Upload.Vars.latestOnlineY))): coordinates_display = coordinates_display + " " coordinates_display = coordinates_display + "\n" coordinates_display = coordinates_display + "Z: " + str( Upload.Vars.latestOnlineZ) for i in range(0, 10 - len(str(Upload.Vars.latestOnlineZ))): coordinates_display = coordinates_display + " " Graphics.GUI.set_online_coordinates(coordinates_display)
def main(): correctorOn = True oldSeg = False CNN_OCR = True plateImg = cv2.imread('./data/input/test5.png') height, width = plateImg.shape[:2] warped = four_point_transform( plateImg, np.array([(0, 0), (width, 0), (width, height), (0, height)])) if oldSeg: validChars = OldSegmentation.segment(plateImg) else: validChars = Segmentation.startSegment(plateImg) print(str(len(validChars))) plateText = OCR.readPlate(validChars, correctorOn, CNN_OCR) if plateText != 'ignore': print(plateText) else: print('Try Again!')
def login(): conn = httplib.HTTPConnection(host) conn.request("GET", "/xk") res = conn.getresponse() res.read() conn.request("GET", "/xk/image.do") res = conn.getresponse() if res.getheader("Set-Cookie") is not None: headers['Cookie'] = res.getheader("Set-Cookie").split(";")[0] with open("captcha.jpg", "wb") as F: F.write(res.read()) # OCR OCRHdl = OCR.CaptchaHandler() rand = OCRHdl.captcha_from_image("captcha.jpg") # POST form data = urllib.urlencode({ "studentId": studentId, "password": password, "rand": rand, "Submit2": r"提交" }) try: time.sleep(0.2) conn.request("POST", "/xk/loginServlet", data, headers) res = conn.getresponse() F = open("out.html", "w") F.write(res.read()) F.close() except Exception as e: print 'Exception' return False conn.close() result = res.reason == "Found" if result: OCRHdl.save_attribute_codes([i for i in rand]) return result
def read(self): self.view.t1.delete(0,len(self.view.t1.get())) self.view.t2.delete(0,len(self.view.t2.get())) self.view.t3.delete(0,len(self.view.t3.get())) self.view.t4.delete(0,len(self.view.t4.get())) self.view.t5.delete(0,len(self.view.t5.get())) self.view.t6.delete(0,len(self.view.t6.get())) self.view.t7.delete(0,len(self.view.t7.get())) self.view.t8.delete(0,len(self.view.t8.get())) self.view.t9.delete(0,len(self.view.t9.get())) file_name = tk.filedialog.askopenfilename() OCR.OCR(file_name) data = NLP.NLP() self.view.t1.insert(0,data[3]) self.view.t2.insert(0,data[4]) self.view.t3.insert(0,data[0]) self.view.t4.insert(0,data[5]) self.view.t5.insert(0,data[7]) self.view.t6.insert(0,data[1]) self.view.t7.insert(0,data[2]) self.view.t8.insert(0,data[6]) self.view.t9.insert(0,data[8])
def get_patient_info(diff_imgs): '''Gets a unique hash of a patient name, the date that the x-ray was taken and the gender of the patient. Arguments: diff_imgs: Array of multiple copies of the same x-ray, each copy has a different type of image processing applied to it to affect the text in the image differently. Returns: patient_id: Unique hash of the patient name. xray_datetime: Date and time that the x-ray was taken. gender: Sex of the patient [M, F]. ''' patient_names = [] patient_births = [] xray_datetimes = [] genders = [] for img in diff_imgs: # run OCR patient_name, patient_birth, xray_datetime, gender = ocr.get_text_from_tesseract( img) patient_names += [patient_name] patient_births += [patient_birth] xray_datetimes += [xray_datetime] genders += [gender] # get the most likely string from all images patient_name = get_popular_string(patient_names) patient_birth = get_popular_string(patient_births) translator = str.maketrans('', '', string.punctuation) patient_birth = str(patient_birth).translate(translator) xray_datetime = get_popular_string(xray_datetimes) gender = get_popular_string(genders) # encrypt patient name to_encrypt = patient_name + patient_birth to_encrypt = to_encrypt.encode('utf-8') patient_id = hashlib.sha256(to_encrypt).hexdigest()[:20] return patient_id, xray_datetime, gender
def api_root(): if request.method == "GET": vidid = request.args.get('id').strip('https://www.youtube.com/embed/') seconds = request.args.get('time') url = request.args.get('id').replace('embed/', 'watch?v=') fname = vidid + ".mp4" folder = "./videos" path = os.path.join(folder, fname) print(url, path, seconds) resp = jsonify(ocr.process_time(url, path, seconds)) print(resp) else: if request.headers['Content-Type'] != 'application/json': return "Please post a JSON" data = json.loads(json.dumps(request.json)) vidid = data['id'].strip('https://www.youtube.com/embed/') url = data['id'].replace('embed/', 'watch?v=') # TODO check if in DB fname = vidid + ".mp4" folder = "./videos" path = os.path.join(folder, fname) if not os.path.isfile(path): vp.downloadYt(url, path) resp = jsonify({}) return resp
def startGUI(): cap = capture.Capture(0) print('before run') cap.start() t = time.time() while cap.img is None: print('waiting for camera to connect',time.time()-t) time.sleep(.25) proc = preprocess.Processor() proc.start(cap) while proc.img is None: print('waiting for processed image',time.time()-t) time.sleep(.25) ocr = OCR.OCR() ocr.start(proc) while ocr.txt is None: print('waiting for OCR',time.time()-t) time.sleep(.25) root.cap = cap root.proc = proc root.mainloop()
#!/usr/bin/env python import OCR OCR.parse_args(__file__) prog = OCR.OCR_prog() blocks = 2 #4 xes_per_blk = 4 #8 db_size = 1024 num_edts = blocks * xes_per_blk * 1 #0 main_dbs = [OCR.DB("db_" + str(n), db_size) for n in range(0, num_edts)] child_edts = [OCR.EDT("child" + str(i)) for i in range(0, xes_per_blk)] child_joiner = OCR.EDT("child_join", deps=child_edts) comp_edts = [ OCR.EDT("comp_" + str(n), deps=[main_dbs[n]], scope=[child_joiner, child_edts]) for n in range(0, num_edts) ] joiner_edt = OCR.EDT("joiner", deps=comp_edts, finish=True) main_edt = OCR.EDT("main", scope=[main_dbs, comp_edts, joiner_edt]) print prog.to_str()
y += 3 if key & 0xFF == ord('d'): x += 3 if key & 0xFF == ord('a'): s -= 3 if key & 0xFF == ord('q'): break if key & 0xFF == ord('t'): t += 2 if key & 0xFF == ord('g'): t -= 2 if key & 0xFF == ord('y'): thresh = -thresh if key & 0xFF == ord('p'): cv2.imwrite('capture.png', croppedTop) textTop = OCR.giveMeText() print "Read Text From Top of Card:" + textTop mostSimilarNameTop, errorDistanceTop = findMostSimilar(textTop, nameList) print "Most Similar :" + mostSimilarNameTop + ", with error distance :" + str(errorDistanceTop) cv2.imwrite('capture.png', croppedBottom) textBottom = OCR.giveMeText() print "Read Text From Bottom of Card:" + textBottom mostSimilarNameBottom, errorDistanceBottom = findMostSimilar(textBottom, nameList) print "Most Similar :" + mostSimilarNameBottom + ", with error distance :" + str(errorDistanceBottom) if (mostSimilarNameTop != ''): if (mostSimilarNameBottom != ''): if (errorDistanceTop <= errorDistanceBottom): print "Top is closer to a real magic card name" else: print "Bottom is closer to a real magic card name"
dataset[contador][8]=float(dataset[contador][8]) dataset[contador][9]=float(dataset[contador][9]) dataset[contador][10]=float(dataset[contador][10]) dataset[contador][11]=float(dataset[contador][11]) dataset[contador][12]=float(dataset[contador][12]) dataset[contador][13]=float(dataset[contador][13]) dataset[contador][14]=float(dataset[contador][14]) dataset[contador][15]=float(dataset[contador][15]) dataset[contador][16]=float(dataset[contador][16]) contador+=1 Matrix=dataset [fila,columna]=img.shape dato1=OCR.Relacion(img) dato2=OCR.Area(img) [dato3,negro1]=OCR.lineaVertical(fila,columna,img) [dato4,negro2]=OCR.lineaHorizontal(fila,columna,img) [x1,x2,x3,x4]=OCR.lineasVerticalesyHorizontales2(img) dato5=OCR.lineasHorizontales5(img) dato6=OCR.lineasVerticales5(img) dato7=OCR.lineasHorizontales3(img) dato8=OCR.lineasVerticales3(img) dato9=x1+x2 #cuantos "1" encontre en los dos cortes dato10=x3+x4# cuantos cambios de valor encontre en los dos cortes dato11=negro1 dato12=negro2 dato13=(dato3+dato4)/fila # relacion entre los cambios quedetecte en una cruz en la imagen entre las filas [dato14,dato15]=OCR.pixelesNegrosYBlancos(fila,columna,img) prediccion=[dato1,dato2,dato3,dato4,dato5,dato6,dato7,dato8,dato9,dato10,dato11,dato12,dato13,dato14,dato15]
def process(self, image): scan = OCR.recognize(image) self.code.add_scan(scan) return self.code
print("[6]Correcting defects") CONTOUR, HIER = cv2.findContours(RES, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) POINTS = [] for CNT in CONTOUR: mom = cv2.moments(CNT) (x, y) = int(mom['m10']/mom['m00']), int(mom['m01']/mom['m00']) cv2.circle(IMG, (x, y), 4, (0, 255, 0), -1) POINTS.append((x, y)) if len(POINTS) != 100: print "[!]Centroids: " + str(len(POINTS)) print "[!]Exiting" sys.exit() C_POINTS = [sorted(i, key = lambda x: x[1]) for i in OCR.split_len(sorted(POINTS), 10)] R_POINTS = [ list(i) for i in zip(*C_POINTS)] R_POINTS = [x for sublist in R_POINTS for x in sublist] ###OCR Stage print "[7]OCR Stage: " RESULT = [[0]*9 for i in range(9)] for row in range(9): 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)
contour, hier = cv2.findContours(res,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) ##create a list with the coordinates sorted centroids = [] for cnt in contour: mom = cv2.moments(cnt) (x,y) = int(mom['m10']/mom['m00']), int(mom['m01']/mom['m00']) centroids.append((x,y)) if len(centroids) != 100: print "Centroids: " + str(len(centroids)) exit() c_points = [sorted(i, key = lambda x: x[1]) for i in OCR.split_len(sorted(centroids),10)] r_points = [ list(i) for i in zip(*c_points)] r_points = [x for sublist in r_points for x in sublist] ###OCR Stage print "7) OCR Stage: " result = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]] 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
###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 "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)