class ALPR(ContextEngineBase): # Trained classifier alpr = None; # Top n highest confidence predictions n = 5 def __init__(self, complexity, numInputs, outputClassifier, inputClassifiers, appFieldsDict): ContextEngineBase.__init__(self,complexity, numInputs, outputClassifier, inputClassifiers, appFieldsDict) self.alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/home/pi/openalpr/runtime_data") if not self.alpr.is_loaded(): print("Error loading OpenALPR") sys.exit(1) self.alpr.set_top_n(self.n) self.alpr.set_default_region("va") # Execute the trained classifier against the given test sample # inputObsVector is a path to the video file def execute(self, inputObsVector): if(len(inputObsVector) == self.numInputs): y_Test = self.predict(inputObsVector); return y_Test; else: print("Wrong dimensions, fail to execute"); return None; # Grabs frames and returns top n predictions per frame. def predict(self, x_Test): cap = cv2.VideoCapture(x_Test[0]) if not cap.isOpened(): print("vid open error") cap.open() fps = 25 timedelta = 0 detectCounter = [0] detectCounter[0] = 0 plates_list = np.empty([0, self.n]) while(cap.isOpened()): ret, frame = cap.read() if (detectCounter[0] < fps*timedelta): detectCounter[0] += 1 continue detectCounter[0] = 0 if ret: pretime = time.time() ret, enc = cv2.imencode("*.bmp", frame) results = self.alpr.recognize_array(bytes(bytearray(enc))) posttime = time.time() plates = np.empty([1,self.n], dtype='a5') for s in range(0, self.n): plates[0][s] = "" for plate in results['results']: i = 0 for candidate in plate['candidates']: platenum = candidate['plate'].encode('ascii','ignore') plates[0][i] = platenum i += 1 timedelta = posttime - pretime # in seconds plates_list = np.vstack((plates_list, plates)) else: break return plates_list;
class AlprPredict: def __init__(self, task_queue, result_queue): self.alpr = Alpr('eu', '/usr/share/openalpr/config/openalpr.defaults.conf', '/usr/share/openalpr/runtime_data') self.alpr.set_top_n(1) self.alpr.set_default_region('lt') self.task_queue = task_queue self.result_queue = result_queue self.run() def run(self): while True: self.predict() def predict(self): image = self.task_queue.get() result = None try: recog_results = self.alpr.recognize_array(image)['results'] if len(recog_results) != 0: result = recog_results[0]['plate'] except Exception as e: logging.error(e) result['status'] = 0 self.result_queue.put(result)
def get_license_plates(image_array): """ This function takes an image array and uses the APLR software to scan for license plates """ alpr = Alpr('us', '/etc/openalpr/openalpr.conf', '/usr/share/openalpr/runtime_data') results = alpr.recognize_array(image_array) redis_dict = {} plates = [] try: for i in range(len(results["results"])): tmp_dict = {} tmp_dict.update({"plate": results["results"][i]["plate"]}) tmp_dict.update( {"confidence": results["results"][i]["confidence"]}) plates.append(tmp_dict) except: print("License plate not found") redis_dict.update({"plates": plates}) coordinates = getLatLon(image_array) redis_dict.update({ "latitude": coordinates[0], "longitude": coordinates[1] }) return redis_dict
def coordRetrv(conf, runtime, image_location): # configure ALPR setting according to config file alpr = Alpr("us", conf, runtime) # Tests if ALPR is able to open if not alpr.is_loaded(): print("Error loading OpenALPR") sys.exit(1) # Gets the top result from ALPR for each plate alpr.set_top_n(1) alpr.set_default_region("tx") # Loads results from the openALPR library # jpeg_bytes = open(image_location, "rb").read() # img_bytes = image_location.tobytes() success, img_numpy = cv2.imencode('.jpg', image_location) img_binary = img_numpy.tostring() results = alpr.recognize_array(img_binary) result = results['results'] # print(result) # Prints the number of license plates # print(len(result)) # TODO: Figure out why this isnt working(maybe needs to be called at end of main # alpr.unload() # return list of all license plates in picture according to ALPR return result
def openALPR(): #reading the picture print('STARTED: ALPR') try: global database alpr = None alpr = Alpr('gb', '/etc/openalpr/openalpr.conf', '/home/pi/openalpr/runtime_data') if not alpr.is_loaded(): print("Error loading OpenALPR") return False else: database = [] alpr.set_top_n(7) alpr.set_default_region("gb") alpr.set_detect_region(False) jpeg_bytes = open('Plates/Plate.jpg', "rb").read() #testing results = alpr.recognize_array(jpeg_bytes) i = 0 for plate in results['results']: i += 1 for candidate in plate['candidates']: plate = [candidate['plate'], candidate['confidence']] database.append(plate) if database == []: remove(filepath) print('FINISHED: ALPR unsucessful') return False else: print(database) print('FINISHED: ALPR sucessful') return True except AttributeError: print()
def faceDetection(self, frame): global value global percentage alpr = Alpr("pak", "path/config/openalpr.conf", "path/openalpr/runtime_data") frame = cv2.resize(frame, (740, 480)) faces = plateCascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) self.allfaces = faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 4) cv2.putText(frame, str(value) + "-" + str(percentage) + "%", (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) if not alpr.is_loaded(): print("Error loading OpenALPR") else: print("Using OpenALPR " + alpr.get_version()) alpr.set_top_n(7) alpr.set_default_region("wa") alpr.set_detect_region(False) cv2.imwrite("1.png", frame) jpeg_bytes = open("1.png", "rb").read() results = alpr.recognize_array(jpeg_bytes) print("Image size: %dx%d" % (results['img_width'], results['img_height'])) print("Processing Time: %f" % results['processing_time_ms']) #print(str(results['results'][0][0]['candidates']['plate'])) i = 0 count = 0 for plate in results['results']: i = 1 print("Plate #%d" % i) print(" %12s %12s" % ("Plate", "Confidence")) for candidate in plate['candidates']: prefix = "-" if candidate['matches_template']: prefix = "*" if count >= 1: break print( " %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence'])) value = candidate['plate'] percentage = candidate['confidence'] count = count + 1 self.bbFrame = frame
def test_postimg(self): alpr = Alpr("eu", "conf/openalpr.conf", "runtime_data") imageFileName = "samples_test/eu-3.jpg" jpeg_bytes = open(imageFileName, "rb").read() results = alpr.recognize_array(jpeg_bytes) post = insight.postimg(imageFileName, "1231231231231231231", str(results['results']), "http://localhost:8080", "admin", "admin") open(imageFileName, "rb").close() self.assertTrue(post)
def start_capture_plates(rtsp): cap = cv2.VideoCapture(rtsp) global lastPlate alpr = Alpr("mx", "openalpr.conf", "runtime_data") if not alpr.is_loaded(): print("Error al Cargar Librería: OpenALPR") alpr.set_top_n(20) alpr.set_default_region("mx") if alpr.is_loaded(): while cap.isOpened(): ret, img = cap.read() img_str = cv2.imencode('.jpg', img)[1].tostring() # cv2.imshow('img', img) results = alpr.recognize_array(img_str) print(results) for plate in results['results']: len_plate = len(plate['plate']) if len_plate == 7: cv2.putText(img, plate['plate'], (plate['coordinates'][0]['x'], plate['coordinates'][0]['y']), 0, 2, (255, 0, 0), 3) cv2.rectangle(img, (plate['coordinates'][0]['x'], plate['coordinates'][0]['y']), (plate['coordinates'][2]['x'], plate['coordinates'][2]['y']), (255, 0, 0), 3) cv2.imshow('img', img) report_plate = black_list(plate['plate']) if report_plate.status == 200: access_tagger(plate['plate']) lastPlate = plate['plate'] response_json = { 'message': 'Acceso Permitido', 'payload': { 'plate': lastPlate, 'description': 'Lorem Ipsum Dolor' } } alpr.unload() cv2.destroyAllWindows() return jsonify(response_json) else: alpr.unload() cv2.destroyAllWindows() response_json = report_plate return response_json
def main(): try: print("Starting...") alpr = Alpr(country, config, runtime_data) if not alpr.is_loaded(): print("Error loading OpenALPR") else: print("Using OpenALPR " + alpr.get_version()) alpr.set_top_n(1) alpr.set_detect_region(False) # initialize the video stream and allow the cammera sensor to warmup video_source = (0 if options["videosource"] == None else options["videosource"]) vs = VideoStream(usePiCamera=options["picamera"] > 0, src=video_source).start() time.sleep(2.0) _frame_number = 0 print("Running...") # loop over the frames from the video stream while True: frame = vs.read() #frame = imutils.resize(frame) #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _frame_number += 1 if _frame_number % FRAME_SKIP == 0: frame_array = (cv2.imencode(".jpg", frame)[1]).tostring() results = alpr.recognize_array(frame_array) if len(results["results"]) > 0: pool.apply_async(_validate, args=[frame_array, results, device, iot, storage]) if options["imshow"]: # show the frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break except: print("[main] Unexpected error:", sys.exc_info()) finally: if alpr: alpr.unload() pool.close() cv2.destroyAllWindows() vs.stop()
def plater(image_paths) -> bool: alpr = None found_count = 0 image_dir = "./images/" try: alpr = Alpr("us", "./baller.alpr.config", "/usr/share/openalpr/runtime_data") if not alpr.is_loaded(): print("Error loading OpenALPR") else: print("Using OpenALPR " + alpr.get_version()) alpr.set_top_n(5) alpr.set_default_region("tx") alpr.set_detect_region(True) for entry in image_paths: jpeg_bytes = open(os.path.join(image_dir, entry), "rb").read() results = alpr.recognize_array(jpeg_bytes) # print ("kakakak \n\n\n") # print (results['results']) # for plate in results['results']: # i += 1 # print("Plate #%d" % i) # print(" %12s %12s" % ("Plate", "Confidence")) # for candidate in plate['candidates']: # prefix = "-" # if candidate['matches_template']: # prefix = "*" # print(" %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence'])) if len(results['results']) > 0: found_count += 1 return found_count > 0 finally: if alpr: alpr.unload()
def start_capture_plates_blacklist(data): cap = cv2.VideoCapture(data['rtsp']) global lastPlate alpr = Alpr("mx", "openalpr.conf", "runtime_data") if not alpr.is_loaded(): print("Error al Cargar Librería: OpenALPR") alpr.set_top_n(20) alpr.set_default_region("mx") if alpr.is_loaded(): while cap.isOpened(): ret, img = cap.read() img_str = cv2.imencode('.jpg', img)[1].tostring() # cv2.imshow('img', img) results = alpr.recognize_array(img_str) # print(results) for plate in results['results']: len_plate = len(plate['plate']) if len_plate == 7: cv2.putText(img, plate['plate'], (plate['coordinates'][0]['x'], plate['coordinates'][0]['y']), 0, 2, (255, 0, 0), 3) cv2.rectangle(img, (plate['coordinates'][0]['x'], plate['coordinates'][0]['y']), (plate['coordinates'][2]['x'], plate['coordinates'][2]['y']), (255, 0, 0), 3) # cv2.imshow('img', img) response_json = black_list_insert(data, plate['plate']) print(response_json) lastPlate = plate['plate'] alpr.unload() cv2.destroyAllWindows() return response_json
def recognize(image): alpr = Alpr("eu", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data") #if not alpr.is_loaded(): #print("Error loading OpenALPR") #else: #print("Using OpenALPR " + alpr.get_version()) #alpr.set_top_n(7) #alpr.set_default_region("eu") #alpr.set_detect_region(False) jpeg_bytes = image.tobytes() results = alpr.recognize_array(jpeg_bytes) if len(results["results"]) > 0: recognition_results = RecognitionResult(results) return True, recognition_results else: return False, None
def alpr(): start_time = time.time(); try: print 'recieved image, processing...' alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data") alpr.set_top_n(20) mid1_time = time.time(); if 'image' not in request.files: print "image was not in files" return 'Image parameter not provided' jpeg_bytes = request.files['image'].read() if len(jpeg_bytes) <= 0: print "there are no bytes!" return False mid2_time = time.time(); results = alpr.recognize_array(jpeg_bytes) print "got results!" end_time = time.time(); print("total_time: " + str(end_time-start_time)); print("alpr_time: " + str(mid1_time-start_time)); print("jpeg_time: " + str(mid2_time-mid1_time)); print("processing_time: " + str(end_time-mid2_time)); return jsonify(results) except Exception, e: print e raise e
def handle_requests(socket): # Load SSD model ssd_model = load_SSD_model() # Load trained tensorflow car classifier and set tensorflow configs tf_config = K.tf.ConfigProto() tf_config.gpu_options.per_process_gpu_memory_fraction = CarConfig.crcl[ "classifier_gpu_memory_frac"] K.set_session(K.tf.Session(config=tf_config)) init = K.tf.global_variables_initializer() sess = K.get_session() sess.run(init) use_plate_recognition = CarConfig.crcl["enable_plate_recognition"] if use_plate_recognition: print("Plate recognition is on, loading OpenALPR..") # Load configs and Alpr() once country = PlateConfig.recognition['country'] region = PlateConfig.recognition['region'] openalpr_conf_dir = PlateConfig.recognition['openalpr_conf_dir'] openalpr_runtime_data_dir = PlateConfig.recognition[ 'openalpr_runtime_data_dir'] top_n = PlateConfig.recognition['top_n'] # Compile regex that matches with invalid TR plates tr_plate_regex = PlateConfig.recognition["tr_plate_regex"] plate_pattern = re.compile(tr_plate_regex) alpr = Alpr(country, openalpr_conf_dir, openalpr_runtime_data_dir) if not alpr.is_loaded(): print("Error loading OpenALPR") return alpr.set_top_n(top_n) alpr.set_default_region(region) # Load model once car_classifier_model, car_classifier_loaded_model_json = load_model_and_json( ) print('Loaded both models successfully, ready to roll.') print('Server is started on:', tcp_address) while True: try: request = socket.recv() image = zmq_comm.decode_request(request) found_objects = [] with isess.as_default(): found_objects = extract_objects(ssd_model, image) clasifications = [] with sess.as_default(): # Filter cars, buses and trucks, classify each of them for o in found_objects: # Crop image according to empirical margin values cropped = crop_image(image, o['topleft'], o['bottomright'], float(o['confidence'])) if cropped is None: continue # Save a copy for plate recognition original_cropped_img = cropped # cv2.imwrite('/home/taylan/Desktop/res/' + str(uuid.uuid4()) + '.jpg', cropped) # Preprocess the image cropped = cropped * 1. / 255 cropped = cv2.resize(cropped, (299, 299)) cropped = cropped.reshape((1, ) + cropped.shape) # Feed image to classifier preds = car_classifier_model.predict(cropped)[0] predict_list = classifyIndices( car_classifier_loaded_model_json, preds, CarConfig.crcl["n"]) predictions = [] tags = ["model", "score"] for index, p in enumerate(predict_list): predictions.append( dict(zip(tags, [p.name, str(p.score)]))) # If we are very sure about the found car's model, try to find its plate as well found_plate = "" if use_plate_recognition and float( predictions[0]["score"]) > 0.75: results = alpr.recognize_array( bytes( cv2.imencode('.jpg', original_cropped_img)[1])) # print("Results: ", results) filtered_candidates = [] for i, plate in enumerate(results['results']): for candidate in plate['candidates']: # print(candidate['plate']) # If our regex does matches with a plate, then it is a good candidate #WARNING: Since our regex expects spaces and openalpr does not put #any spaces in found plate string, it never matches if plate_pattern.search(candidate['plate']): filtered_candidates.append( candidate['plate']) # WARNING: It is assumed that there is only a single plate in the given image # Hence, we break after the first plate, even if there are more plates break # print(filtered_candidates) if len(filtered_candidates) > 0: found_plate = filtered_candidates[0] cl = { 'label': o['label'], 'confidence': o['confidence'], 'topleft': o['topleft'], 'bottomright': o['bottomright'], 'predictions': predictions, 'plate': found_plate } clasifications.append(cl) result_dict = {} result_dict["result"] = clasifications result_dict["message"] = "OK" # print(result_dict) socket.send_json(result_dict) except Exception as e: result_dict = {} result_dict["result"] = [] result_dict["message"] = str(e) socket.send_json(result_dict)
format_ = "P5" else: return return "%s %d %d 255 " % (format_, width, height) + im2.tostring() if __name__ == "__main__": if v.isOpened(): rval, frame = v.read() else: rval = False while rval: # recognize if n % skip == 0: result = a.recognize_array(convert2pnm(frame)) for r in result['results']: pprint(r['plate']) # If the plate is registered, allow acces and take a snapshoot if r['plate'] == 'AB123CD': print('Authorized') cv2.imwrite('plate.png', frame) time.sleep(5) cv2.imshow(r['plate'], frame) # resize factor = frame.shape[1] / float(width) height = int(frame.shape[0]/factor) resized = cv2.resize(frame, (width, height)) # show resized frame cv2.imshow("video", resized)
video_writer = cv2.VideoWriter('tailgate2_video.avi',fourcc,5,(600,500)) widthArray = [1 for k in range(4)] heightArray = [1 for h in range(4)] j = 0 count = 0 while count < 5: count = count + 1 stream = io.BytesIO(); with picamera.PiCamera() as camera: camera.resolution = (600,500); camera.capture(stream, format ='bmp') buff = numpy.fromstring(stream.getvalue(),dtype = numpy.uint8) image = cv2.imdecode(buff,1) results = alpr.recognize_array(bytes(bytearray(buff))) if count == 1: video_writer.write(image) cv2.imshow('Video', image) for plate in results['results']: cv2.putText(frame, "Plate#: " + plate['plate'], (0,22), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,0), 2) cv2.putText(frame, "Tailgating!!!", (0,100), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255), 2) coordinatesFile = open('tailgate.log', 'a') coordinatesFile.write('Driver tailgate '), coordinatesFile.write('%s\n' % (datetime.datetime.now())) coordinatesFile.close() for coordinates in results['results']: for x in coordinates['coordinates']:
from openalpr import Alpr alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data") print(alpr.is_loaded()) results = alpr.recognize_file("/home/ubuntu/ea7the.jpg") print(results) with open("/home/ubuntu/ea7the.jpg", 'rb') as im_file: im_bytes = im_file.read() results = alpr.recognize_array(im_bytes) print(results)
def main(): alpr = Alpr('gb', '/srv/openalpr/openalpr.conf', '/srv/openalpr/runtime_data') if not alpr.is_loaded(): print('Error loading OpenALPR') sys.exit(1) alpr.set_top_n(3) #alpr.set_default_region('new') cap = open_cam_rtsp(RTSP_SOURCE) if not cap.isOpened(): alpr.unload() sys.exit('Failed to open video file!') #cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_AUTOSIZE) #cv2.setWindowTitle(WINDOW_NAME, 'OpenALPR video test') _frame_number = 0 #declare a stamp 10 minutes ago, initialise variable when script starts STAMP = datetime.datetime.now() - datetime.timedelta(minutes=10) while True: ret_val, frame = cap.read() if not ret_val: print(datetime.datetime.now()) print('VidepCapture.read() failed. Exiting...') break _frame_number += 1 if _frame_number % FRAME_SKIP != 0: continue #cv2.imshow(WINDOW_NAME, frame) ret, enc = cv2.imencode("*.jpg", frame) results = alpr.recognize_array(enc.tobytes()) #results = alpr.recognize_ndarray(frame) for i, plate in enumerate(results['results']): best_candidate = plate['candidates'][0] print(datetime.datetime.now() + ' Plate #{}: {:7s} ({:.2f}%)'.format(i, best_candidate['plate'].upper(), best_candidate['confidence'])) #Does the plate match known plates if best_candidate['plate'].upper() in PLATES: #Has the gate fired recently? If not in the last 10 minutes then allow to fire again if datetime.datetime.now() > STAMP: print("Recognised") #If a plate is recongised set a timestamp to prevent it firing lots of times until time has expired print(datetime.datetime.now()) #Open the gate response = requests.post( HA_ENDPOINT, headers={'Content-Type': 'application/json', 'x-ha-access': '' + HA_APIPASS + '' }, data='{"entity_id": "script.ANPR"}', ) STAMP = STAMP = datetime.datetime.now() + datetime.timedelta(minutes=3) else: print("Not firing as time has not exceeded") if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cap.release() alpr.unload()
options = parser.parse_args() alpr = None try: alpr = Alpr(options.country, options.config, options.runtime_data) if not alpr.is_loaded(): print("Error loading OpenALPR") else: print("Using OpenALPR " + alpr.get_version()) alpr.set_top_n(7) alpr.set_default_region("wa") alpr.set_detect_region(False) jpeg_bytes = open(options.plate_image, "rb").read() results = alpr.recognize_array(jpeg_bytes) # Uncomment to see the full results structure # import pprint # pprint.pprint(results) print("Image size: %dx%d" %(results['img_width'], results['img_height'])) print("Processing Time: %f" % results['processing_time_ms']) i = 0 for plate in results['results']: i += 1 print("Plate #%d" % i) print(" %12s %12s" % ("Plate", "Confidence")) for candidate in plate['candidates']: prefix = "-"
alpr.set_top_n(20) alpr.set_default_region("md") cap = cv2.VideoCapture( "rtsp://*****:*****@192.168.1.109:554/cam/realmonitor?channel=1&subtype=0" ) ret, frame = cap.read() ret, enc = cv2.imencode("*.bmp", frame) while (True): ret, frame = cap.read() cv2.imshow('frame', frame) # cv.waitKey(1) # gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) results = alpr.recognize_array(bytes(bytearray(enc))) if cv2.waitKey(1) & 0xFF == ord('q'): break # results = alpr.recognize_ndarray(gray) print results['results'] i = 0 for plate in results['results']: i += 1 print "Plate #%d" % i print " %12s %12s" % ("Plate", "Confidence") for candidate in plate['candidates']: prefix = "-" if candidate['matches_template']: prefix = "*"
class Alpr_detection(object): def __init__(self, alpr_dir, country=None, config_file=None, runtime_dir=None): config_path = alpr_dir + "/openalpr.conf" runtime_path = alpr_dir + "/runtime_data" country_code = "eu" if config_file is not None: config_path = config_file if runtime_dir is not None: runtime_path = runtime_dir if country is not None: country_code = country environ["PATH"] = alpr_dir + ";" + environ["PATH"] self.__alpr = Alpr(country_code, config_path, runtime_path) def is_loaded(self): return self.__alpr.is_loaded() # configuration&alpr # def alpr_dir(self, alpr_dir): # environ["PATH"] = alpr_dir + ";" + environ["PATH"] # alpr = Alpr("eu", alpr_dir + "/openalpr.conf", alpr_dir + "/runtime_data") # plate frame # def alpr_detection(self): # video = cv2.VideoCapture(0) # catch video # best_score = [] # k = 0 # while True: # # catch video # flag, frame = video.read() # cv2.imshow('Clear_frame', frame) # cv2.waitKey(2) # if not flag: # print('Video read failed.') # # results = alpr.recognize_ndarray(frame) # flag, enc = cv2.imencode("*.jpg", frame) # ndarray issue # # results = alpr.recognize_array(bytes(bytearray(enc))) # ndarray issue # results = self.__alpr.recognize_array(enc.tobytes()) def detection_photo(self, filePath): jpeg_bytes = open(filePath, "rb").read() return self.__alpr.recognize_array(jpeg_bytes) def detection_video(self, video, showCapFrame): # video = cv2.VideoCapture(0) # catch video # catch video flag, frame = video.read() # width, height, c = frame.shape # frame = frame[450:width, 0:height] if showCapFrame is True: if frame is None: return cv2.imshow('Clear_frame', frame) cv2.waitKey(1) if not flag: print('Video read failed.') return None # results = alpr.recognize_ndarray(frame) flag, enc = cv2.imencode("*.jpg", frame) # ndarray issue # results = alpr.recognize_array(bytes(bytearray(enc))) # ndarray issue results = self.__alpr.recognize_array(enc.tobytes()) return results, frame # print results # return results, frame # print(results['results']) # def # hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # converting BGR to HSV # lower_red = np.array([30, 150, 50]) # define range of red color in HSV # upper_red = np.array([255, 255, 180]) # mask = cv2.inRange(hsv, lower_red, upper_red) # create a red HSV colour boundary and threshold HSV image # res = cv2.bitwise_and(frame, frame, mask=mask) # Bitwise-AND mask and original image # cv2.imshow('Original', frame) # Display an original image # edges = cv2.Canny(frame, 100, 200) # finds edges in the input image image and marks them in the output map edges # cv2.imshow('Original', edges) # output edges # crop_frame = frame[0:480, 320:640] # cv2.imshow("Crop_frame", crop_frame) # crop_frame = frame[0:500, 0:640*2] # cv2.imshow("Crop_frame", crop_frame) def crop_frame(self, results, frame): if len(results['results']) == 0: z = 0 # result # print('No plate detected') else: y, y1 = json.dumps( results["results"][0]["coordinates"][0]["y"]), json.dumps( results["results"][0]["coordinates"][3]["y"]) x, x1 = json.dumps( results["results"][0]["coordinates"][0]["x"]), json.dumps( results["results"][0]["coordinates"][1]["x"]) crop_frame = frame[int(y):int(y1), int(x):int(x1)] cv2.rectangle(frame, (int(x) - 10, int(y) - 10), (int(x1), int(y1)), (0, 255, 0), 3) # cv2.imshow("suka", crop_frame) # cv2.waitKey(2) return crop_frame # crop_frame_canny = cv2.Canny(crop_frame, 100, 200) # contours, hierarchy = cv2.findContours(crop_frame_canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # for cnt in contours: # rect = cv2.minAreaRect(cnt) # box = cv2.boxPoints(rect) # box = np.int0(box) # area = int(rect[1][0] * rect[1][1]) # if area > 500: # cv2.drawContours(frame, [box], 0, (0, 255, 0), 3) # crop_frame = frame[int(x):int(x1), int(y):int(y1)] def print_results(self, frame, crop_frame, results): # cv2.imshow("Crop_frame", frame) # cv2.waitKey(2) if crop_frame is None: return # cv2.imshow("True frame", crop_frame) # cv2.waitKey(2) # cv2.imshow("Crop_frame", frame) # cv2.waitKey(2) for i, plate in enumerate(results['results']): best_candidate = plate['candidates'][0] if best_candidate['confidence'] > 85: # for b in enumerate[best_score[b]]: best_score = best_candidate['confidence'] # k += 1 # if k == 2: # print("plate: " + str(best_candidate['plate'].upper())) # exit() print(best_candidate['confidence']) print("plate: " + str(best_candidate['plate'].upper()))
class AlprDetector: def __init__(self, name, config, video_source, event_callback=None, save_images=False): self.__name = name self.__alpr_instance = Alpr(config.region, config.config_file, config.runtime_data_file) if not self.__alpr_instance.is_loaded(): print('Alpr instance could not be created') self.__frame_skip = config.frame_skip self.event_callback = event_callback self.__video_source = video_source self.__cap = cv2.VideoCapture(video_source) self.__running = False self.__save_image = save_images import os self.__directory = os.getcwd() def is_working(self): return self.__cap.isOpened() and self.__running def video_source_properties(self): data = dict() data['source'] = self.__video_source data['fps'] = self.__cap.get(cv2.CAP_PROP_FPS) data['width'] = self.__cap.get(cv2.CAP_PROP_FRAME_WIDTH) data['height'] = self.__cap.get(cv2.CAP_PROP_FRAME_HEIGHT) data['codec'] = self.__cap.get(cv2.CAP_PROP_FOURCC) return @staticmethod def __extract_results(alpr_result): if alpr_result is None: return None if not alpr_result['results']: return None else: result = [] for plate in alpr_result['results']: for candidate in plate['candidates']: result.append( [candidate['plate'], candidate['confidence']]) return result @staticmethod def __extract_best_candidate(alpr_result): return alpr_result['results'][0]['plate'] if alpr_result[ 'results'] else None def __handle_results(self, extracted_results): if self.event_callback is not None: callback_data = dict() # object is list of dict containing 'plate' and 'confidence' callback_data['candidates'] = extracted_results callback_data['detector'] = self.__name print('calling callback , ', extracted_results) self.event_callback(callback_data) def run(self): if self.__running: print(self.__name, ' Detector is already running') return False else: self.__running = True if not self.is_working(): print(self.__name, ' Video capture not working.') self.__running = False return False frame_number = 0 last_recognized_plate = None error_state = False try: print(self.__name, ' starting detector loop for: ', self.__video_source) while self.__running: a = datetime.datetime.now() last_read_status, frame = self.__cap.read() if not last_read_status: print('Video capture.read() failed. Stopping the work') self.__running = False error_state = True break frame_number += 1 if frame_number % self.__frame_skip == 0: frame_number = 0 continue if cv2.waitKey(1) == 27: break # cv2.imshow(self.__name, frame) # todo: use recognize_ndarray when updated to at least 2.3.1 # alpr.recognize_ndarray(frame) ret, enc = cv2.imencode("*.bmp", frame) results = self.__alpr_instance.recognize_array( bytes(bytearray(enc))) best_candidate = self.__extract_best_candidate(results) if best_candidate is not None and best_candidate != last_recognized_plate: last_recognized_plate = best_candidate print(best_candidate) # send first recognized plate and all candidates extracted_results = self.__extract_results(results) if extracted_results: self.__handle_results(extracted_results) if self.__save_image: print(self.__directory) import os.path cv2.imwrite( os.path.join( self.__directory, self.__name, ''.join( (best_candidate, '_', self.__name, '_', datetime.datetime.now().strftime( "%Y_%m_%d_%H_%M_%S"), '.jpeg'))), frame) except cv2.error as e: print("OpenCV Exception caught: ", e) error_state = True except Exception as e: print("Exception caught: ", e) error_state = True finally: self.__alpr_instance.unload() self.__running = False print(self.__name, " is stopping") return not error_state def stop(self): self.__running = False
def f(data): parser = ArgumentParser(description='OpenALPR Python Test Program') parser.add_argument("-c", "--country", dest="country", action="store", default="us", help="License plate Country" ) OpenALPR_path = "C:/Users/Franco/Documents/Github/control-vehicular/Otros/Deteccion/openalpr_32bit/" parser.add_argument("--config", dest="config", action="store", default=OpenALPR_path+"openalpr.conf", help="Path to openalpr.conf config file" ) parser.add_argument("--runtime_data", dest="runtime_data", action="store", default=OpenALPR_path+"runtime_data", help="Path to OpenALPR runtime_data directory" ) #parser.add_argument('plate_image', help='License plate image file') options = parser.parse_args() print(options.country, options.config, options.runtime_data) alpr = None try: alpr = Alpr(options.country.encode('ascii'), options.config.encode('ascii'), options.runtime_data.encode('ascii')) if not alpr.is_loaded(): print("Error loading OpenALPR") else: print("Using OpenALPR " + alpr.get_version().decode('ascii')) alpr.set_top_n(7) alpr.set_default_region(b"wa") alpr.set_detect_region(False) # jpeg_bytes = open(options.plate_image, "rb").read() # results = alpr.recognize_array(jpeg_bytes) jpeg_bytes = data results = alpr.recognize_array(bytes(bytearray(data))) # Uncomment to see the full results structure # import pprint # pprint.pprint(results) print("Image size: %dx%d" %(results['img_width'], results['img_height'])) print("Processing Time: %f" % results['processing_time_ms']) i = 0 if results['results']: print("%12s%12f" % (results['results'][0]['plate'], results['results'][0]['confidence'])) for plate in results['results']: i += 1 print("Plate #%d" % i) print(" %12s %12s" % ("Plate", "Confidence")) for candidate in plate['candidates']: prefix = "-" if candidate['matches_template']: prefix = "*" print(" %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence'])) finally: if alpr: alpr.unload()
class LprRunner(NDUCameraRunner): def __init__(self, config, _connector_type): super().__init__() self.counter = 0 self._send_data = config.get("send_data", False) city_codes_fn = "/data/city_codes.json" if not os.path.isfile(city_codes_fn): city_codes_fn = os.path.dirname( os.path.abspath(__file__)) + city_codes_fn.replace( "/", os.path.sep) with open(city_codes_fn, encoding="UTF-8") as f_in: self._cities = json.load(f_in) conf_fn = "/data/openalpr_64/openalpr.conf" #conf_fn = "//usr/local/share/openalpr/config/openalpr.defaults.conf" # conf_fn = "/data/openalpr_64/runtime_data/config/eu.conf" if not os.path.isfile(conf_fn): conf_fn = os.path.dirname( os.path.abspath(__file__)) + conf_fn.replace("/", os.path.sep) runtime_data = "/data/openalpr_64/runtime_data/" #runtime_data = "/usr/local/share/openalpr/runtime_data/" if not os.path.isdir(runtime_data): runtime_data = os.path.dirname( os.path.abspath(__file__)) + runtime_data.replace( "/", os.path.sep) # self._alpr = Alpr("us", "/path/to/openalpr.conf", "/path/to/runtime_data") # self._alpr = Alpr("eu", conf_fn, runtime_data) self._alpr = Alpr("tr", conf_fn, runtime_data) if not self._alpr.is_loaded(): print("Error loading OpenALPR") # self._alpr.set_top_n(20) # self._alpr.set_default_region("md") # self._alpr.set_top_n(1) self._alpr.set_default_region("tr") self._alpr.set_country("tr") # region lp detection, onnx_fn = "/data/yolov4-tiny_lp_416_static.onnx" self.input_size = 416 if not os.path.isfile(onnx_fn): onnx_fn = os.path.dirname( os.path.abspath(__file__)) + onnx_fn.replace("/", os.path.sep) classes_filename = "/data/class.names" if not os.path.isfile(classes_filename): classes_filename = os.path.dirname( os.path.abspath(__file__)) + classes_filename.replace( "/", os.path.sep) self.class_names = ["lp"] self.sess_tuple = onnx_helper.get_sess_tuple(onnx_fn) # endregion def get_name(self): return "lpr" def get_settings(self): settings = {} return settings def process_frame(self, frame, extra_data=None): def to_bbox(coordinates, rect_, rh_, rw_): x1 = coordinates[0]["x"] * rw_ y1 = coordinates[0]["y"] * rh_ x2 = coordinates[2]["x"] * rw_ y2 = coordinates[2]["y"] * rh_ if rect_ is not None: x1 += rect_[1] y1 += rect_[0] x2 += rect_[1] y2 += rect_[0] return [y1, x1, y2, x2] def enumerate_images(frame_): result = yolo_helper.predict_v4(self.sess_tuple, self.input_size, self.class_names, frame) for _class_name, _score, rect0, item_ in NDUUtility.enumerate_result_items( result, return_item=True): rect1 = geometry_helper.add_padding_rect(rect0, 0.5) yield image_helper.crop(frame, rect1), rect0, item_ res = [] for image, rect, item in enumerate_images(frame): h0, w0 = image_helper.image_h_w(image) # def order_points(pts): # # initialzie a list of coordinates that will be ordered # # such that the first entry in the list is the top-left, # # the second entry is the top-right, the third is the # # bottom-right, and the fourth is the bottom-left # rect = np.zeros((4, 2), dtype="float32") # # # the top-left point will have the smallest sum, whereas # # the bottom-right point will have the largest sum # s = pts.sum(axis=1) # rect[0] = pts[np.argmin(s)] # rect[2] = pts[np.argmax(s)] # # # now, compute the difference between the points, the # # top-right point will have the smallest difference, # # whereas the bottom-left will have the largest difference # diff = np.diff(pts, axis=1) # rect[1] = pts[np.argmin(diff)] # rect[3] = pts[np.argmax(diff)] # # # return the ordered coordinates # return rect # # def four_point_transform(image, pts): # # obtain a consistent order of the points and unpack them # # individually # rect = order_points(pts) # (tl, tr, br, bl) = rect # # # compute the width of the new image, which will be the # # maximum distance between bottom-right and bottom-left # # x-coordiates or the top-right and top-left x-coordinates # widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2)) # widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2)) # maxWidth = max(int(widthA), int(widthB)) # # # compute the height of the new image, which will be the # # maximum distance between the top-right and bottom-right # # y-coordinates or the top-left and bottom-left y-coordinates # heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) # heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) # maxHeight = max(int(heightA), int(heightB)) # # # now that we have the dimensions of the new image, construct # # the set of destination points to obtain a "birds eye view", # # (i.e. top-down view) of the image, again specifying points # # in the top-left, top-right, bottom-right, and bottom-left # # order # dst = np.array([ # [0, 0], # [maxWidth - 1, 0], # [maxWidth - 1, maxHeight - 1], # [0, maxHeight - 1]], dtype="float32") # # # compute the perspective transform matrix and then apply it # M = cv2.getPerspectiveTransform(rect, dst) # warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight)) # return warped # # def deskew(image): # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # gray = cv2.bitwise_not(gray) # thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # coords = np.column_stack(np.where(thresh > 0)) # angle = cv2.minAreaRect(coords)[-1] # if angle < -45: # angle = -(90 + angle) # else: # angle = -angle # (h, w) = image.shape[:2] # center = (w // 2, h // 2) # M = cv2.getRotationMatrix2D(center, angle, 1.0) # return cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) # # # def remove_noise_and_smooth(file_name): # # img = cv2.imread(file_name, 0) # # filtered = cv2.adaptiveThreshold(img.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 41) # # kernel = np.ones((1, 1), np.uint8) # # opening = cv2.morphologyEx(filtered, cv2.MORPH_OPEN, kernel) # # closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) # # img = image_smoothening(img) # # or_image = cv2.bitwise_or(img, closing) # # return or_image # # # image = deskew(image) # # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) h1, w1 = image_helper.image_h_w(image) while w1 < 400: image = cv2.pyrUp(image) h1, w1 = image_helper.image_h_w(image) # cv2.imshow("lpr", image) # cv2.waitKey(500) success, encoded_image = cv2.imencode('.jpg', image) content2 = encoded_image.tobytes() results = self._alpr.recognize_array(content2) added = False #print("LPR: ", item.get(constants.RESULT_KEY_RECT)) for plate in results['results']: txt = plate["plate"] if len(txt) > 2: score = plate["confidence"] / 100.0 if score > 0.01: city_code = txt[0:2] city_name = None if city_code in self._cities: city_name = self._cities[city_code] if city_name is None: class_name = "PL: {}".format(txt) else: class_name = "PL: {} {}".format(city_name, txt) val = { constants.RESULT_KEY_RECT: rect, constants.RESULT_KEY_SCORE: score, constants.RESULT_KEY_CLASS_NAME: class_name } if self._send_data: if city_name is None: val[constants.RESULT_KEY_DATA] = {"pl": txt} else: val[constants.RESULT_KEY_DATA] = { "pl": txt, "city": city_name } res.append(val) added = True if not added: val = { constants.RESULT_KEY_RECT: rect, constants.RESULT_KEY_SCORE: 0, constants.RESULT_KEY_CLASS_NAME: "PL: " } res.append(val) return res
alpr.set_default_region("md") capture = cv2.VideoCapture(VIDEO_CAPTURE) # is_to_send = 0 while (True): if cv2.waitKey(1) & 0xFF == ord('q'): break frame = None while frame is None: et, frame = capture.read() print("processing frame") ret, image = cv2.imencode('.bmp', frame) results = alpr.recognize_array(bytes(bytearray(image))) print(results) print("done") i = 0 hypotheses = [] for plate in results['results']: i += 1 print("Plate #%d" % i) print(" %12s %12s" % ("Plate", "Confidence")) for j, candidate in enumerate(plate['candidates']): if j > 3: break prefix = "-" if candidate['matches_template']:
class ALPR(ContextEngineBase): # Trained classifier alpr = None # Top n highest confidence predictions n = 5 def __init__(self, complexity, numInputs, outputClassifier, inputClassifiers, appFieldsDict): ContextEngineBase.__init__(self, complexity, numInputs, outputClassifier, inputClassifiers, appFieldsDict) self.alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/home/pi/openalpr/runtime_data") if not self.alpr.is_loaded(): print("Error loading OpenALPR") sys.exit(1) self.alpr.set_top_n(self.n) self.alpr.set_default_region("va") # Execute the trained classifier against the given test sample # inputObsVector is a path to the video file def execute(self, inputObsVector): if (len(inputObsVector) == self.numInputs): y_Test = self.predict(inputObsVector) return y_Test else: print("Wrong dimensions, fail to execute") return None # Grabs frames and returns top n predictions per frame. def predict(self, x_Test): cap = cv2.VideoCapture(x_Test[0]) if not cap.isOpened(): print("vid open error") cap.open() fps = 25 timedelta = 0 detectCounter = [0] detectCounter[0] = 0 plates_list = np.empty([0, self.n]) while (cap.isOpened()): ret, frame = cap.read() if (detectCounter[0] < fps * timedelta): detectCounter[0] += 1 continue detectCounter[0] = 0 if ret: pretime = time.time() ret, enc = cv2.imencode("*.bmp", frame) results = self.alpr.recognize_array(bytes(bytearray(enc))) posttime = time.time() plates = np.empty([1, self.n], dtype='a5') for s in range(0, self.n): plates[0][s] = "" for plate in results['results']: i = 0 for candidate in plate['candidates']: platenum = candidate['plate'].encode('ascii', 'ignore') plates[0][i] = platenum i += 1 timedelta = posttime - pretime # in seconds plates_list = np.vstack((plates_list, plates)) else: break return plates_list
print(address) f = open('file.jpg', 'wb') # open in binary l = 1 while (l): l = sc.recv(1024) while (l): f.write(l) l = sc.recv(1024) f.close() #alpr.set_top_n(20) alpr.set_detect_region(True) f = open("file.jpg", "rb") jpeg_bytes = f.read() results = alpr.recognize_array(jpeg_bytes) # for plate in results['results']: # for candidate in plate['candidates']: # print(candidate['plate']) # print(plate['candidates'][0]['plate']) result = results['results'][0]['candidates'][0]['plate'] print(result) sc.send(result.encode('utf-8')) # i = 0 # for plate in results['results']: # i += 1 # print("Plate #%d" % i) # print(" %12s %12s" % ("Plate", "Confidence"))
class LicensePlateSearcher: def __init__(self, plate, country=DEFAULT_COUNTRY): self.plate = plate self.alpr = Alpr(country, ALPR_CONFIG_PATH, ALPR_RUNTIME_DATA_PATH) self.trashhold = 0.70 self.photo_maker = Process(target=self.photogapher, ) @property def _current_time(self): now = datetime.datetime.now() return "{:>02}:{:>02}:{:>02}".format(now.hour, now.minute, now.second) @staticmethod def _get_patterns_similarity(pattern_a, pattern_b): similarity = SequenceMatcher(None, pattern_a, pattern_b).ratio() return round(similarity, 2) def _get_plates(self): with open('image.jpg', 'rb') as image: jpg_bytes = image.read() alpr_report = self.alpr.recognize_array(jpg_bytes) alpr_results = alpr_report['results'] plates = [result['plate'] for result in alpr_results] return plates def _get_plates_validity(self, plates): for plate in plates: similarity = self._get_patterns_similarity(plate, self.plate) if similarity >= self.trashhold: return plate return None def _process_photo(self): try: plates = self._get_plates() except IndexError: print "No numbers, at {}".format(self._current_time) else: valid_plate = self._get_plates_validity(plates) if valid_plate is None: print "No correct numbers, at {}".format(self._current_time) else: print "{} at {}".format(valid_plate, self._current_time) def photogapher(self): stream = urllib.urlopen(MJPG_STREAM_URL) stream_fragment = '' while True: stream_fragment += stream.read(8096) photo_start_ptr = stream_fragment.find('\xff\xd8') photo_end_ptr = stream_fragment.find('\xff\xd9') if photo_start_ptr < photo_end_ptr and photo_start_ptr != -1 and photo_end_ptr != -1: jpg = stream_fragment[photo_start_ptr:photo_end_ptr + 2] stream_fragment = stream_fragment[photo_end_ptr + 2:] image = cv2.imdecode(numpy.fromstring(jpg, dtype=numpy.uint8), cv2.IMREAD_COLOR) cv2.imwrite('image.jpg', image) elif photo_end_ptr > photo_start_ptr: stream_fragment = '' else: pass def run(self): run_command( 'mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 1280x720 -y 1 -n" -o "output_http.so -p 8080 -w /usr/share/mjpg-streamer/www/"' ) time.sleep(1) self.photo_maker.start() time.sleep(1) while True: self._process_photo()