def __convert_and_store_train_docs_as_IOB(self, train_request, train_doc_source_path): train_request = parse_train_request(train_request) type_system = load_typesystem(train_request.typeSystem) dataset_name = '{0}_{1}_{2}_{3}'.format(train_request.projectId, train_request.userId, train_request.layer, train_request.feature) doc_dir = train_doc_source_path.format(dataset_name) # Todo: need to use train_doc_target_path param later # Todo: at some later point some of the files should be added to dev set; and use an empty test set; # Todo: For now we will use const dev and train set; may be check if we can use empty test set or not if not os.path.exists(doc_dir): os.makedirs(doc_dir) for doc in train_request.documents: try: text, file_name = doc.documentText, doc.documentId file_path = '{0}/{1}'.format(doc_dir, file_name) if os.path.exists(file_path): # print('file: {0} already exists'.format(file_path)) log_info(msg='file: {0} already exists'.format(file_path), token_id=self.training_token, prefix=self.prefix) continue assert len(text) > 0 cas = load_cas_from_xmi( text.encode('utf-8').decode('unicode-escape'), typesystem=type_system) output = extract_IOB_from_CAS(cas, train_request.feature, train_request.layer) with open(file_path, 'w') as op: for (word, token) in output: op.writelines('{0} {1}\n'.format(word, token)) op.flush() except Exception: log_error(msg="=" * 120, token_id=self.training_token, prefix=self.prefix) log_error( msg= "Exception in function convert_and_store_train_docs_as_IOB() for CAS file: {0}" .format(file_name), token_id=self.training_token, prefix=self.prefix) log_error(msg="=" * 120, token_id=self.training_token, prefix=self.prefix) log_error(msg=text, token_id=self.training_token, prefix=self.prefix) log_error(msg="=" * 120, token_id=self.training_token, prefix=self.prefix) log_error(msg=output, token_id=self.training_token, prefix=self.prefix) log_error(msg="=" * 120, token_id=self.training_token, prefix=self.prefix) log_error(msg=traceback.format_exc(), token_id=self.training_token, prefix=self.prefix) log_error(msg="=" * 120, token_id=self.training_token, prefix=self.prefix) return dataset_name
def train(self, train_request, training_config): self.training_token = str(uuid.uuid4()) self.prefix = training_config['common_prefix'] if not self._lock.acquire(False): return False, "Busy!!! Already a Training Ongoing" else: try: dataset_name = self.__convert_and_store_train_docs_as_IOB( train_request, training_config['train_document_source']) training_config.update({ 'token': self.training_token, 'dataset_name': dataset_name }) self.train_file_options = { # 'train_set_size': self.train_set_size, 'train_document_source': training_config['train_document_source'].format( dataset_name), 'train_document_path': training_config['train_document_path'].format( dataset_name), 'train_history_file': training_config['train_history_file'].format(dataset_name), 'incremental_training': training_config['is_incremental'], 'new_dataset': not os.path.exists("models/{0}.{1}".format( dataset_name, 'h5')), 're_run_old_data': False, 're_run_offset': 0, 'training_document_threshold': training_config['training_document_threshold'] } is_trainable, result_msg = self.__can_train() if is_trainable: tr = InceptionModelTrainer(training_config) # log_debug('Trainer id={0}'.format(id(tr))) # log_debug('tr members {0}'.format(dir(tr))) # tr.begin() # log_debug('after tr.begin() members {0}', dir(tr)) has_error, result_msg = tr.train_model() # log_debug('after tr.train_model() members {0}', dir(tr)) # tr.tear_down() # log_debug('after tr.tear_down() members', dir(tr)) # log_debug('network dictionary id={0}, data={1}'.format(id(training_config['network_dict']),training_config['network_dict'])) # tr.train_model() # tr.__exit__() # Todo: Try to use it using with block; but was not successful so far # with InceptionModelTrainer(training_config) as trainer: # print('trainer',dir(trainer)) # has_error, result_msg = trainer.train_model() if has_error: log_error(msg=result_msg, token_id=self.training_token, prefix=self.prefix) else: log_info(msg=result_msg, token_id=self.training_token, prefix=self.prefix) else: return is_trainable, result_msg except Exception: log_error(msg="!!!!! Ex: {0}".format(traceback.format_exc()), token_id=self.training_token, prefix=self.prefix) finally: self._lock.release() return has_error, result_msg
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, counting_line, line_position, mcdf): # add new blobs or update existing ones matched_blob_ids = [] for i in range(len(boxes)): _type = classes[i] if classes != None else None #print (confidences) if confidences != []: _confidence = confidences[i] else: _confidence=None box_centroid = get_centroid(boxes[i]) box_area = get_area(boxes[i]) match_found = False for _id, blob in blobs.items(): #print (_id,blob) if blob.counted == False and get_iou(boxes[i], blob.bounding_box) > 0.5: match_found = True if _id not in matched_blob_ids: blob.num_consecutive_detection_failures = 0 matched_blob_ids.append(_id) temp_blob = create_blob(boxes[i], _type, _confidence, frame, tracker) # TODO: update blob w/o creating temp blob blob.update(temp_blob.bounding_box, _type, _confidence, temp_blob.tracker) #blob.trajectory # Create a sequence of points to make a contour #contour=cv2.contourArea([(100,100),(200,200),(500,500)]) #cv2.pointPolygonTest([(100,100),(200,200),(500,500)], box_centroid, false) #area_of_left_left = np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left_straight=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left_right=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) # area_of_left=np.array([[1, 1], [10, 50], [50, 50]], dtype=np.int32) #left_left = object_in_polygon(frame, area_of_left_left,box_centroid) #print(box_centroid) #print(left_left) log_info('Blob updated.', { 'event': 'BLOB_UPSERT', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'type': blob.type, 'type_confidence': blob.type_confidence, 'center': box_centroid, #'direction': direction #'trajectory':trajectory.append #'image': get_base64_image(get_box_image(frame, blob.bounding_box)) }) f = './ProcessRecords/Blobupdated58.txt' with open(f, "a") as file: file.write( 'BLOB_UPSERT' + '-' + 'id' + str(_id) + '-' + 'bounding_box'+str(blob.bounding_box)+'-'+'type' + str(blob.type) + '-' + 'type_confidence' + str( blob.type_confidence) + '-' + 'center' + str( box_centroid) + "\n") break if not match_found and not is_passed_counting_line(box_centroid, counting_line, line_position): _blob = create_blob(boxes[i], _type, _confidence, frame, tracker) blob_id = generate_vehicle_id() blobs[blob_id] = _blob log_info('Blob created.', { 'event': 'BLOB_UPSERT', 'vehicle_id': blob_id, 'bounding_box': _blob.bounding_box, 'type': _blob.type, 'type_confidence': _blob.type_confidence, 'center': box_centroid #'image': get_base64_image(get_box_image(frame, _blob.bounding_box)) }) f = './ProcessRecords/Blobcreated58.txt' with open(f, "a") as file: file.write( 'BLOB_UPSERT' + '-' + 'id' + str(blob_id) + '-' + 'bounding_box' + str( _blob.bounding_box) + '-' + 'type' + str(_blob.type) + '-' + 'type_confidence' + str( _blob.type_confidence) + '-' + 'center' + str(box_centroid) + "\n") blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf) return blobs
def run(): ''' Loads environment variables, initializes counter class and runs counting loop. ''' # capture traffic scene video is_cam = ast.literal_eval(os.getenv('IS_CAM')) video = int(os.getenv('VIDEO')) if is_cam else os.getenv('VIDEO') cap = cv2.VideoCapture(video) if not cap.isOpened(): log_error('Error capturing video. Invalid source.', { 'cat': 'VIDEO_CAPTURE', 'source': video }) ret, frame = cap.read() f_height, f_width, _ = frame.shape detection_interval = int(os.getenv('DI')) mcdf = int(os.getenv('MCDF')) mctf = int(os.getenv('MCTF')) detector = os.getenv('DETECTOR') tracker = os.getenv('TRACKER') # create detection region of interest polygon use_droi = ast.literal_eval(os.getenv('USE_DROI')) droi = ast.literal_eval(os.getenv('DROI')) \ if use_droi \ else [(0, 0), (f_width, 0), (f_width, f_height), (0, f_height)] show_droi = ast.literal_eval(os.getenv('SHOW_DROI')) counting_line_position = os.getenv('COUNTING_LINE_POSITION') vehicle_counter = VehicleCounter(frame, detector, tracker, droi, show_droi, mcdf, mctf, detection_interval, counting_line_position) record = ast.literal_eval(os.getenv('RECORD')) headless = ast.literal_eval(os.getenv('HEADLESS')) if record: # initialize video object to record counting output_video = cv2.VideoWriter(os.getenv('OUTPUT_VIDEO_PATH'), \ cv2.VideoWriter_fourcc(*'MJPG'), \ 30, \ (f_width, f_height)) log_info( 'Processing started...', { 'cat': 'COUNT_PROCESS', 'counter_config': { 'di': detection_interval, 'mcdf': mcdf, 'mctf': mctf, 'detector': detector, 'tracker': tracker, 'use_droi': use_droi, 'droi': droi, 'show_droi': show_droi, 'clp': counting_line_position } }) # main loop while is_cam or cap.get(cv2.CAP_PROP_POS_FRAMES) + 1 < cap.get( cv2.CAP_PROP_FRAME_COUNT): if ret: vehicle_counter.count(frame) output_frame = vehicle_counter.visualize() if record: output_video.write(output_frame) if not headless: debug_window_size = ast.literal_eval( os.getenv('DEBUG_WINDOW_SIZE')) resized_frame = cv2.resize(output_frame, debug_window_size) cv2.imshow('Debug', resized_frame) k = cv2.waitKey(1) & 0xFF if k == ord('s'): # save frame if 's' key is pressed take_screenshot(output_frame) if k == ord('q'): # end video loop if 'q' key is pressed log_info('Processing stopped.', {'cat': 'COUNT_PROCESS'}) break ret, frame = cap.read() # end capture, close window, close log file and video object if any cap.release() if not headless: cv2.destroyAllWindows() if record: output_video.release() log_info('Processing ended.', {'cat': 'COUNT_PROCESS'})
def count(self, frame): log = [] self.frame = frame for _id, blob in list(self.blobs.items()): # update trackers success, box = blob.tracker.update(self.frame) if success: blob.num_consecutive_tracking_failures = 0 blob.update(box) log_info('Vehicle tracker updated.', { 'event': 'TRACKER_UPDATE', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'centroid': blob.centroid, }) else: blob.num_consecutive_tracking_failures += 1 # count vehicles that have left the frame if no counting line exists # or those that have passed the counting line if one exists if (self.counting_line == None and \ (blob.num_consecutive_tracking_failures == self.mctf or blob.num_consecutive_detection_failures == self.mcdf) and \ not blob.counted) \ or \ (self.counting_line != None and \ # don't count a blob if it was first detected at a position past the counting line # this enforces counting in only one direction not is_passed_counting_line(blob.position_first_detected, self.counting_line, self.cl_position) and \ is_passed_counting_line(blob.centroid, self.counting_line, self.cl_position) and \ not blob.counted): blob.counted = True self.vehicle_count += 1 # count by vehicle type if blob.type != None: if blob.type in self.types_counts: self.types_counts[blob.type] += 1 else: self.types_counts[blob.type] = 1 log_info('Vehicle counted.', { 'event': 'VEHICLE_COUNT', 'id': _id, 'type': blob.type, 'count': self.vehicle_count, 'position_first_detected': blob.position_first_detected, 'position_counted': blob.centroid, 'counted_at':time.time(), }) if blob.num_consecutive_tracking_failures >= self.mctf: # delete untracked blobs del self.blobs[_id] if self.frame_count >= self.di: # rerun detection droi_frame = get_roi_frame(self.frame, self.droi) _bounding_boxes, _classes, _confidences = get_bounding_boxes(self.yolo,droi_frame, self.detector) self.blobs = add_new_blobs(_bounding_boxes, _classes, _confidences, self.blobs, self.frame, self.tracker, self.counting_line, self.cl_position, self.mcdf) self.blobs = remove_duplicates(self.blobs) self.frame_count = 0 self.frame_count += 1 return log
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, counting_line, line_position, mcdf): # add new blobs or update existing ones matched_blob_ids = [] for i in range(len(boxes)): _type = classes[i] if classes != None else None _confidence = confidences[i] if confidences != None else None _tracker = get_tracker(tracker, boxes[i], frame) box_centroid = get_centroid(boxes[i]) box_area = get_area(boxes[i]) match_found = False for _id, blob in blobs.items(): if blob.counted == False and get_iou(boxes[i], blob.bounding_box) > 0.5: match_found = True if _id not in matched_blob_ids: blob.num_consecutive_detection_failures = 0 matched_blob_ids.append(_id) blob.update(boxes[i], _type, _confidence, _tracker) log_info( 'Blob updated.', { 'cat': 'BLOB_UPSERT', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'type': blob.type, 'type_confidence': blob.type_confidence, 'image': get_base64_image( get_box_image(frame, blob.bounding_box)) }) break if not match_found and not is_passed_counting_line( box_centroid, counting_line, line_position): _blob = Blob(boxes[i], _type, _confidence, _tracker) blob_id = generate_vehicle_id() blobs[blob_id] = _blob log_info( 'Blob created.', { 'cat': 'BLOB_UPSERT', 'vehicle_id': blob_id, 'bounding_box': _blob.bounding_box, 'type': _blob.type, 'type_confidence': _blob.type_confidence, 'image': get_base64_image(get_box_image(frame, _blob.bounding_box)) }) blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf) return blobs
def add_new_blobs(boxes, classes, confidences, blobs, frame, tracker, counting_line, line_position, mcdf): # add new blobs or update existing ones matched_blob_ids = [] print(classes) print(boxes) for i in range(len(boxes)): if classes != []: print(classes[i]) _type = classes[i] else: _type=None print (confidences) if confidences != []: _confidence = confidences[i] else: _confidence=None box_centroid = get_centroid(boxes[i]) box_area = get_area(boxes[i]) match_found = False for _id, blob in blobs.items(): #print (_id,blob) if blob.counted == False and get_iou(boxes[i], blob.bounding_box) > 0.5: match_found = True if _id not in matched_blob_ids: blob.num_consecutive_detection_failures = 0 matched_blob_ids.append(_id) temp_blob = create_blob(boxes[i], _type, _confidence, frame, tracker) # TODO: update blob w/o creating temp blob blob.update(temp_blob.bounding_box, _type, _confidence, temp_blob.tracker) #blob.trajectory # Create a sequence of points to make a contour #contour=cv2.contourArea([(100,100),(200,200),(500,500)]) #cv2.pointPolygonTest([(100,100),(200,200),(500,500)], box_centroid, false) log_info('Blob updated.', { 'event': 'BLOB_UPSERT', 'vehicle_id': _id, 'bounding_box': blob.bounding_box, 'type': blob.type, 'type_confidence': blob.type_confidence, 'center': box_centroid, #'direction': direction #'trajectory':trajectory.append #'image': get_base64_image(get_box_image(frame, blob.bounding_box)) }) break if not match_found and not is_passed_counting_line(box_centroid, counting_line, line_position): _blob = create_blob(boxes[i], _type, _confidence, frame, tracker) blob_id = generate_vehicle_id() blobs[blob_id] = _blob log_info('Blob created.', { 'event': 'BLOB_UPSERT', 'vehicle_id': blob_id, 'bounding_box': _blob.bounding_box, 'type': _blob.type, 'type_confidence': _blob.type_confidence, 'centere': box_centroid #'image': get_base64_image(get_box_image(frame, _blob.bounding_box)) }) blobs = remove_stray_blobs(blobs, matched_blob_ids, mcdf) return blobs