def main(): capture = cv.VideoCapture(0) assert capture.isOpened(), "Cannot connect to camera" capture.set(cv.CAP_PROP_FPS, 5) capture.set(cv.CAP_PROP_FRAME_WIDTH, 640) capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480) matching = FeatureMatching(train_image='train.png') for success, frame in iter(capture.read, (False, None)): cv.imshow("frame", frame) match_succsess, img_warped, img_flann = matching.match(frame) if match_succsess: cv.imshow("res", img_warped) cv.imshow("flann", img_flann) if cv.waitKey(1) & 0xff == 27: break
class FeatureMatchingLayout(BaseLayout): """A custom layout for feature matching display A plain GUI layout for feature matching output. Each captured frame is passed to the FeatureMatching class, so that an object of interest can be tracked. """ def _init_custom_layout(self): """Initializes feature matching class""" self.matching = FeatureMatching(train_image='salinger.jpg') def _create_custom_layout(self): """Use plain layout""" pass def _process_frame(self, frame): """Processes each captured frame""" # if object detected, display new frame, else old one success, new_frame = self.matching.match(frame) if success: return new_frame else: return frame
def testing(): target = os.path.join(APP_ROOT, 'images/') print(target) if not os.path.isdir(target): os.mkdir(target) else: print("Couldn't create upload directory: {}".format(target)) print(request.files.getlist("file")) for upload in request.files.getlist("file"): print(upload) print("{} is the file name".format(upload.filename)) filename = upload.filename destination = "/".join([target, filename]) print("Accept incoming file:", filename) print("Save it to:", destination) upload.save(destination) start = datetime.now() start_time_data = time.strftime("%Y/%m/%d %H:%M:%S") # image_path = 'images/taxi2.jpg' image_path = 'images/' + filename img = cv2.imread(image_path) # try: # for orientation in ExifTags.TAGS.keys(): # if ExifTags.TAGS[orientation] == 'Orientation': # break # exif = dict(img._getexif().items()) # # if exif[orientation] == 3: # img = img.rotate(180, expand=True) # elif exif[orientation] == 6: # img = img.rotate(270, expand=True) # elif exif[orientation] == 8: # img = img.rotate(90, expand=True) # # except (AttributeError, KeyError, IndexError): # # cases: image don't have getexif # pass single_template = cv2.imread('static/img/template-img/single.png') double_template = cv2.imread('static/img/template-img/double.png') feature_template = cv2.imread('static/img/template-img/feature.png') is_ev_car = 0 is_general_car = 0 cropped_plate_img = None valid_crop = 1 with open(image_path, 'rb') as binary_image: cropped_plate_img = func.crop_plate_img(binary_image.read(), img) if cropped_plate_img is False: valid_crop = 0 print('Error while cropping plate img', valid_crop) return render_template("complete_test.html", status=valid_crop) #gray_plate_img = cv2.cvtColor(cropped_plate_img, cv2.COLOR_BGR2GRAY) # Template Matching plate with one small evcar shape image single_template_matching = TemplateMatching(cropped_plate_img, single_template) single_template_matching.find_position() # Template Matching plate with special EV mark from left part of plate double_template_matching = TemplateMatching(cropped_plate_img, double_template) double_template_matching.find_position() # check position of 2 logos. if func.check_position_of_logos(single_template_matching, double_template_matching) is False: is_general_car += 1 print('no EV car (logos not matching)') else: is_ev_car += 1 print('EV car (logos matched)') # draw rectangle in Original image first_img = single_template_matching.draw_rectangle() second_img = double_template_matching.draw_rectangle(first_img) # Feature matching by 3 logos template. feature_template_matching = FeatureMatching(cropped_plate_img, feature_template) # if there is no points of feature matching.plus 1 point(is_general_car) if feature_template_matching.find_points() < 1: is_general_car += 1 print('no EV car (no feature matching detected)') else: is_ev_car += 1 # if there are some points, plus 1 point print('EV car (feature matched) : {}'.format( feature_template_matching.find_points())) # Detect which color of plate # if it is True : is_ev_car, else is_general_car color_of_plate = func.detect_plate_color(cropped_plate_img) if color_of_plate is not True: is_general_car += 1 else: is_ev_car += 1 print("is_general_vehicle_points : {}\nis_eletronic_vehicle_points : {}". format(is_general_car, is_ev_car)) if is_ev_car >= 2: car_type = 'Electronic Vehicle' feature_point_img = feature_template_matching.draw_feature_points() # Set image as max width and same height combined_h = second_img.shape[0] combined_w = max(second_img.shape[1], feature_point_img.shape[1]) new_size = (combined_w, combined_h) # Draw feature matching points on original image second_img = cv2.resize(second_img, new_size) feature_point_img = cv2.resize(feature_point_img, new_size) # Rectangle template image + feature matching image as vertical way result_plate_img = cv2.vconcat([second_img, feature_point_img]) else: car_type = 'General Vehicle' result_plate_img = cropped_plate_img cv2.imwrite('static/results/saved.jpg', result_plate_img) processing = datetime.now() - start end_time_data = time.strftime("%Y/%m/%d %H:%M:%S") print(processing) return render_template("complete_test.html", status=valid_crop, image_name=filename, car=car_type, processing=processing, result_img=result_plate_img, start_time_data=start_time_data, end_time_data=end_time_data)
import cv2 from feature_matching import FeatureMatching #camera's image img_train = cv2.imread('./2.jpg') #goal image query_image = cv2.imread('./1.jpg') matching = FeatureMatching(query_image='./1.jpg') m, flag = matching.match(img_train, query_image) if (m == False): print("not match!!!")
def _init_custom_layout(self): """Initializes feature matching class""" self.matching = FeatureMatching(train_image='salinger.jpg')
# -*- coding:utf-8 -*- __author__ = 'Microcosm' import cv2 from feature_matching import FeatureMatching img_train = cv2.imread('train.png') matching = FeatureMatching(query_image='query.png') flag = matching.match(img_train)
def _init_custom_layout(self): """Initializes feature matching class""" self.matching = FeatureMatching(train_image="salinger.jpg")
real = 13 # write the frame # timestr = time.strftime("%Y_%m%d-%H_%M_%S") # ImageName = "image_" + timestr + "__" + str(times + 1) + ".jpg" # camera's image # img_train = cv2.imread('./' + ImageName) # goal image ImageName = 'picture' + str(times + 1) + '.jpg' cv2.imwrite(ImageName, frame) print("\n save picture" + str(times + 1) + "...\n") img_train = cv2.imread('picture1.jpg') print('picture' + str(times + 1) + '.jpg' + " compare to Goal_1.jpg") query_image = cv2.imread('./Goal_1.jpg') matching = FeatureMatching(query_image='./Goal_1.jpg') elif (GoalNum == 2): width = 7.1 real = 51.4 img_train = cv2.imread('picture' + str(times + 1) + '.jpg') print('picture' + str(times + 1) + '.jpg' + " compare to Goal_2.jpg") query_image = cv2.imread('./Goal_2.jpg') matching = FeatureMatching(query_image='./Goal_2.jpg') elif (GoalNum == 3): width = 7.1 real = 51.4 img_train = cv2.imread('picture' + str(times + 1) + '.jpg') print('picture' + str(times + 1) + '.jpg' + " compare to Goal_3.jpg") query_image = cv2.imread('./Goal_3.jpg')
import cv2 from feature_matching import FeatureMatching img_train = cv2.imread('001.jpg') matching = FeatureMatching(query_image='121.jpg') flag = matching.match(img_train)
if ret == True: # write the frame timestr = time.strftime("%Y_%m%d-%H_%M_%S") ImageName = "image_" + timestr + "__" + str(times + 1) + ".jpg" cv2.imwrite(ImageName, frame) print("\n save picture" + str(times + 1) + "...\n") f.write("\n save picture" + str(times + 1) + "...\n") # camera's image img_train = cv2.imread('./' + ImageName) # goal image if (correct < 3): print(ImageName + " compare to Goal_1.jpg") f.write("\n " + ImageName + " compare to Goal_1.jpg") query_image = cv2.imread('./Goal_1.jpg') matching = FeatureMatching(query_image='./Goal_1.jpg') else: print(ImageName + " compare to Goal_2.jpg") f.write("\n " + ImageName + " compare to Goal_2.jpg") query_image = cv2.imread('./Goal_2.jpg') matching = FeatureMatching(query_image='./Goal_2.jpg') m, x, y = matching.match(img_train, query_image) break except: print( "This picture cannot be taken any special point, and will be tried again.\n" ) f.write( "\n This picture cannot be taken any special point, and will be tried again.\n" )
break # Capture frame-by-frame ret, frame = VideoStream.read() if ret == True: # Display the resulting frame cv2.imshow("camera's streaming video", frame) # write the frame timestr = time.strftime("%Y_%m%d-%H_%M_%S") ImageName = "image_" + timestr + "__" + str(times + 1) + ".jpg" cv2.imwrite(ImageName, frame) print("\n save picture" + str(times + 1) + "...\n") times += 1 # camera's image img_train = cv2.imread('./' + ImageName) # goal image query_image = cv2.imread('./Goal.jpg') matching = FeatureMatching(query_image='./Goal.jpg') m = matching.match(img_train, query_image) if (m == False): print("not match!!!\n") else: break # When everything done, release the capture VideoStream.release() cv2.destroyAllWindows()