def buildQueues(queuesConf): queues = {} for conf in queuesConf: if getWithDefault(conf,"activate",1): q = Queue(maxsize=conf["depth"]) queues[conf["name"]] = q return queues
def buildModels(modelsConf): models = {} for conf in modelsConf: if getWithDefault(conf,"activate",1): model = SSD7Model(conf["name"],conf["weightsPath"]) models[model.getName()] = model return models
def buildCameras(queues,camerasConf): cameras = {} for conf in camerasConf: if getWithDefault(conf,"activate",1): camera = Cam(conf["name"],conf["videoURL"],queues[conf["imageQ"]]) cameras[conf["name"]] = camera camera.start() return cameras
def predict(self, camera, frm, idFrame): ssd_preds, height, width = self.ssd_predictor.predict( camera, frm, idFrame) # ver si no tenemos plate y tenemos un plate (5) en y_pred # si sí invocamos alpr a ver si podemos leer la placa (5 veces) clip_name = "data/images/clip-{}.jpg".format(camera) print("ssd_preds: {}".format(ssd_preds)) trailer_present = False for pred in ssd_preds: print("TRAE: {}".format(pred[0])) if pred[0] == 2: print("SI HAY TRAILER!") trailer_present = True if pred[0] == 5 and self.plate == "unknown": print("TRAE PLACA: {}".format(pred)) xmin = pred[2] ymin = pred[3] xmax = pred[4] ymax = pred[5] cv2.imwrite(clip_name, frm[ymin - 5:ymax + 5, xmin - 5:xmax + 5, :]) for k in range(0, 30): cv2.imwrite(clip_name + str(k) + ".jpg", frm[ymin - k:ymax + k, xmin - k:xmax + k, :]) response = subprocess.check_output([ "alpr", "--config", "config/openalpr.conf", "-n", "1", "-c", "mx", "-p", "mx", clip_name ]).decode("utf-8") print("arlp: {}".format(response)) if response != "No license plates found.\n": response = response.split("\n")[1:][0].split("\t") plate = response[0].replace(" - ", "") confidence = response[1].split(":")[1].strip() is_match = response[2].split(":")[1].strip() if is_match == "1": self.plates.append(plate) best = {} print("PLATES: {}".format(self.plates)) for p in self.plates: cnt = getWithDefault(best, p, 0) best[p] = cnt + 1 if best[p] > 1: self.plate = p print("BEST: {}".format(best)) #cv2.putText(img_disp,plate,(xmin_s,int(ymin-12/scale)), #cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2,cv2.LINE_AA) #print("[{}] {}".format(plate,confidence)) else: print(response) if trailer_present == False: print("NO HAY TRAILERRRRR") self.plates = [] self.plate = "unknown" return (ssd_preds, height, width, self.plate)
def main(configFile, param): cleanResDirectory() config = decodeConfigJson(configFile) #caudalURL = config["Caudal"] serviceLogURL = "http://localhost:8080/logs" print("Sending events to: {}".format(serviceLogURL)) models = M.buildModels(config["Models"]) queues = Q.buildQueues(config["Queues"]) predictors = P.buildPredictors(models, queues, config["Predictors"]) print("creating cameras") cameras = VCS.buildCameras(queues, config["Cameras"]) withImg = True if param == "manual" else False print("mostrando imagenes ?" + str(withImg)) outQ = queues["qr_out"] lastDataMatrixSent = "" n = 0 while 1: print("Sacando elemento {}".format(n)) component, cam, pred, img, ok = outQ.get() n += 1 objects = pred["objects"] dataMatrix = pred["dataMatrix"] plate = getWithDefault(pred, "plate", "unknown") height, width, channels = img.shape if ok and (dataMatrix != "unknown") and lastDataMatrixSent != dataMatrix: lastDataMatrixSent = dataMatrix info = {"cameraId": cam, "aITime": getMillis(), "data": dataMatrix} print("Sending: {}".format(info)) # try: # requests.post( # serviceLogURL, # headers=JSON_HEADERS, # data=json.dumps(info)) # except: # print("Unexpected error:", sys.exc_info()[0]) if withImg: showPrediction(img, pred, scale=2) cmdK = cv2.waitKey(10) & 0xFF if cmdK == ord("q"): print("Quiting...1") streamCams.stopAll() cv2.destroyAllWindows() print("Quiting...2") exit()
def buildPredictors(models, queues, predictorsConf): predictors = {} for conf in predictorsConf: if getWithDefault(conf, "activate", 1): if conf["type"] == "ssd7": model = models[conf["model"]] predictor = SSD7Predictor( conf["name"], model, queues[conf["qIn"]], queues[conf["qOut"]], img_height=getWithDefault(conf, "img_height", 270), img_width=getWithDefault(conf, "img_width", 480), class_threshold=getWithDefault(conf, "class_threshold", None)) else: predictor = DataMatrixDecoder(conf["name"], conf["clipsPath"], queues[conf["qIn"]], queues[conf["qOut"]]) predictors[conf["name"]] = predictor print("GOT THE CONTROL") predictor.start() return predictors
def _run(self): while self.running: predecesor, cam, result, frm, ok = self.inQ.get() result["dataMatrix"] = "unknown" camState = getWithDefault(self.state, cam, { "plates": [], "plate": "unknown" }) self.state[cam] = camState if ok: if len(result["objects"]) >= 1: objects = result["objects"] print(" *******READING {}".format(objects)) pred = objects[0] xmin = pred[2] ymin = pred[3] xmax = pred[4] ymax = pred[5] crop_img = frm[ymin:ymax, xmin:xmax] dataMatrix = self.decodeQrDataMatrix(crop_img) result["dataMatrix"] = dataMatrix #print ("****************** DATA MATRIX " + dataMatrix) #clipName = "qr002.png" #cv2.imwrite(clipName, crop_img) ##dataReaded= decodeQrDataMatrix(crop_img) else: print("nothing to read") self.outQ.put((self.name, cam, result, frm, True)) else: self.outQ.put((self.name, predecesor, { "name": self.name, "predecesor": predecesor, "error": "Terminó" }, None, False)) self.dest_queue.put((self.name, None, None, None, False)) self.running = False self.thr = None return self
def _run(self): while self.running: predecesor, cam, result, frm, ok = self.inQ.get() camState = getWithDefault(self.state, cam, { "plates": [], "plate": "unknown" }) self.state[cam] = camState print("ALPR state: {}".format(self.state[cam])) if ok: trailer_present = False for pred in result["objects"]: if pred[0] == 2: trailer_present = True if pred[0] == 5 and self.state[cam]["plate"] == "unknown": xmin = pred[2] ymin = pred[3] xmax = pred[4] ymax = pred[5] clipName = self.clipsPath + "/{}.jpg" clipName = clipName.format(cam) cv2.imwrite( clipName, frm[ymin - 5:ymax + 5, xmin - 5:xmax + 5, :]) response = subprocess.check_output([ "alpr", "--config", "config/openalpr.conf", "-n", "1", "-c", "mx", "-p", "mx", clipName ]).decode("utf-8") print("arlp: {}".format(response)) if response != "No license plates found.\n": response = response.split("\n")[1:][0].split("\t") plate = response[0].replace(" - ", "") confidence = response[1].split(":")[1].strip() is_match = response[2].split(":")[1].strip() if is_match == "1": self.state[cam]["plates"].append(plate) best = {} print("PLATES: {}".format(self.state[cam])) for p in self.state[cam]["plates"]: cnt = getWithDefault(best, p, 0) best[p] = cnt + 1 if best[p] > 1: self.state[cam]["plate"] = p print("BEST: {}".format(best)) #cv2.putText(img_disp,plate,(xmin_s,int(ymin-12/scale)), #cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2,cv2.LINE_AA) #print("[{}] {}".format(plate,confidence)) if not trailer_present: self.state[cam] = {"plate": "unknown", "plates": []} result["plate"] = self.state[cam]["plate"] print("PUTTING {}".format(result)) self.outQ.put((self.name, cam, result, frm, True)) else: self.outQ.put((self.name, predecesor, { "name": self.name, "predecesor": predecesor, "error": "Terminó" }, None, False)) self.dest_queue.put((self.name, None, None, None, False)) self.running = False self.thr = None return self