def record(state): global config config = state.app.config global text_analyzer text_analyzer = load_class( config['CLASS_TEXT_ANALYZER']).from_config(config) text_analyzer.init() global candy_detector candy_detector = CandyDetector.from_config(config) global candy_classifier candy_classifier = CandyClassifier.from_config(config) candy_classifier.init() global candy_trainer candy_trainer = CandyTrainer.from_config(config) global image_capture image_capture = load_class( config['CLASS_IMAGE_CAPTURE']).from_config(config) global image_calibrator image_calibrator = ImageCalibrator.from_config(config)
import os import sys import time import cv2 import numpy as np sys.path.append('../../webapp') from candysorter.models.images.calibrate import ImageCalibrator from candysorter.models.images.detect import CandyDetector from candysorter.config import get_config calibrator = ImageCalibrator(area=(1625, 1100), scale=550) detector = CandyDetector.from_config(get_config(os.getenv('FLASK_ENV', 'dev'))) should_exit = False def mouse_event(event, x, y, flags, param): global should_exit if event == cv2.EVENT_LBUTTONUP: print("mouse_event:L-click") should_exit = True def write_message(image, msg, size=3, thickness=3): cv2.putText(image, msg, (10, 130), font, size, (250, 30, 30), thickness)
def main(): parser = argparse.ArgumentParser(description='Gather images for training.') parser.add_argument('--image_dir', type=str, default="training_dir", help="location for new training images + /label_name") detector = CandyDetector.from_config(config_dict) td = Trainingdata() args = parser.parse_args() image_dir = args.image_dir candy_list = config_img.BOX_CANDIES if config.CANDY_TYPE == 0: candy_list = config_img.TWIST_CANDIES print('The following labels are available in directory: ') for i in range(len(candy_list)): print(i, ':', candy_list[i]) lid = int(input("input label number: ")) label = "" if lid < 0 or lid >= len(candy_list): raise ValueError("{} is not a valid input".format(lid)) else: label = candy_list[lid] path_to_label_dir = os.path.join(image_dir, label) if not tf.gfile.Exists(path_to_label_dir): tf.gfile.MakeDirs(path_to_label_dir) capture = cv2.VideoCapture(0) capture.set(3, 1920) capture.set(4, 1080) w2_size = (960, 540) cv2.namedWindow('Detection', cv2.WINDOW_KEEPRATIO | cv2.WINDOW_NORMAL) cv2.resizeWindow('Detection', *w2_size) cv2.setMouseCallback('Detection', td.mouse_event) candies = None counter = 0 while True: time.sleep(0.01) if capture.isOpened: ret, frame = capture.read() if not ret: break corners = td.detect_corners(frame) if corners is not None: cropped = calibrator.calibrate(frame) #write_message(cropped, label) #td.write_message(cropped, 'click right mouse button to take image') if (counter % 5 == 0): candies = detector.detect(cropped) td.set_variables( candies, os.path.join(os.path.join(image_dir, label))) td.draw_detection(cropped, candies) cv2.imshow('Detection', cropped) td.write_ok(frame) counter += 1 else: blank = np.zeros((w2_size[1], w2_size[0], 3), np.uint8) td.write_message(blank, 'Marker detection failed', size=1, thickness=1) cv2.imshow('Detection', blank) key = cv2.waitKey(1) if (key == ord(' ')): td.take_pictures_and_save() if (key == 27): break if should_exit: break print("Exit.") cv2.destroyAllWindows()
sys.path.append('../../webapp') from candysorter.models.images.calibrate import ImageCalibrator from candysorter.models.images.detect import CandyDetector from candysorter.config import get_config calibrator = ImageCalibrator(area=(1625, 1100), scale=550) config = get_config(os.getenv('FLASK_ENV', 'dev')) detector = CandyDetector( histgram_band=config.CANDY_DETECTOR_HISTGRAM_BAND, histgram_thres=config.CANDY_DETECTOR_HISTGRAM_THRES, bin_thres=config.CANDY_DETECTOR_BIN_THRES, edge3_thres=config.CANDY_DETECTOR_EDGE3_THRES, edge5_thres=config.CANDY_DETECTOR_EDGE5_THRES, margin=config.CANDY_DETECTOR_MARGIN, closing_iter=config.CANDY_DETECTOR_CLOSING_ITER, opening_iter=config.CANDY_DETECTOR_OPENING_ITER, erode_iter=config.CANDY_DETECTOR_ERODE_ITER, dilate_iter=config.CANDY_DETECTOR_DILATE_ITER, bg_size_filter=config.CANDY_DETECTOR_BG_SIZE_FILTER, sure_fg_thres=config.CANDY_DETECTOR_SURE_FG_THRES, restore_fg_thres=config.CANDY_DETECTOR_RESTORE_FG_THRES, box_dim_thres=config.CANDY_DETECTOR_BOX_DIM_THRES ) should_exit = False def mouse_event(event, x, y, flags, param): global should_exit if event == cv2.EVENT_LBUTTONUP: print("mouse_event:L-click")
sys.path.append('../../webapp') from candysorter.models.images.calibrate import ImageCalibrator from candysorter.models.images.detect import CandyDetector from candysorter.config import get_config calibrator = ImageCalibrator(area=(1625, 1100), scale=550) config = get_config(os.getenv('FLASK_ENV', 'dev')) # Convert config from class object to dictionary for compatability with flask config_dict = {} for key in dir(config): if key.isupper(): config_dict[key] = getattr(config, key) detector = CandyDetector().from_config(config_dict) should_exit = False def mouse_event(event, x, y, flags, param): global should_exit if event == cv2.EVENT_LBUTTONUP: print("mouse_event:L-click") should_exit = True def write_message(image, msg, size=3, thickness=3): cv2.putText(image, msg, (10, 130), font, size, (250, 30, 30), thickness)