Example #1
0
    def run_ocr(self, img_path="./ocr-test6.png", resize_size=0, window_size=22, stride=2, prob_threshold=0.999):
        img = load_img(img_path)
        # Resize
        if resize_size > 0:
            img.thumbnail(resize_size, Image.ANTIALIAS)

        # Display image with a sample box in order to visually determine window_size
        logging.info("\tShowing picture with example box in top-left corner of given window size")
        imc_c = img.copy()
        draw = ImageDraw.Draw(imc_c)
        draw.rectangle([0, 0, window_size, window_size], fill=None, outline="red")
        del draw
        imc_c.show()

        self.run_convnet(batch_size=len(self.prep_funcs), plot=False, epochs=10)
        #self.run_ffnet(plot=False, epochs=20)
        ocr_prep_funcs = [edge_enhance]
        detect(self.convnet, img, ocr_prep_funcs, window_size, stride, prob_threshold)
Example #2
0
def draw_noun_detections_on_video_frames(video_id, wnid):
  '''
  Draws boxes of detections of <wnid> on the frames of the video.
  The boxes have been non-maximally suppressed.

  Because selective search provides about 2000 boxes per frame, and the noun
  usually isn't even present, I don't consider a detection positive if the score
  for the positive class is higher than the negative class. Instead, I
  consider a detection positive if the positive score is > 0.99 (out of 1.00)

  If you want to change the drawing code, but not have to rerun the detection,
  simply delete the directory of annotated images you want to redraw (found in
  the data/imagenet/<wnid>/annotated directory)

  The names of the annotated directories take this form:
    <model>_<video_id>_<ms_betwen_frames>
  '''
  url = video_url(video_id)
  video_filename = fetch_video(url)
  if video_filename is None:
    print video_id, 'skipped'
    return
  image_dir = get_prepared_images(url, FLAGS.ms_between_frames,
                                  video_filename, wnid)
  wnid_dir = join(ROOT, 'data/imagenet', wnid)
  annotated_dir = join(dirname(dirname(MODELS[FLAGS.model][0])),
                       'annotated', basename(image_dir))
  if exists(annotated_dir):
    print 'The annotated dir for ' + video_id + ' already exists'
    return
  system('mkdir -p ' + annotated_dir)

  detections_filename = join('/tmp',
    '_'.join(['detection', 'results', FLAGS.model, wnid, video_id]) + '.bin')
  if not exists(detections_filename):
    print detections_filename, 'does not already exist'
    detect(image_dir, detections_filename,
           MODELS[FLAGS.model][0], MODELS[FLAGS.model][1])
  else:
    print detections_filename, 'already exists'
  if not exists(detections_filename):
    print 'Something went wrong during detection'
    sys.exit(1)
  draw_detection_results(detections_filename, annotated_dir)
Example #3
0
File: main.py Project: ychaim/Final
import cv2
import detector
import bin
import copy
from image_data import ImageData
import characteran
from edgefinder import EdgeFinder
from transformation import Transformation
from textline import TextLine
from segment.ocr import OCR
import numpy as np
from province_detect.state_detector import SateDetector
img = cv2.imread('q.jpeg')
img_data = ImageData(img)
rows, cols = img.shape[:2]
grays, regions = detector.detect(img)
print type(grays[0])
for numberPlate in range(len(grays)):
    img_data.crop_gray = grays[numberPlate]
    img_data.regionOfInterest = regions[numberPlate]
    an_img = copy.copy(img_data.crop_gray)
    img_data.thresholds = bin.produceThresholds(an_img)
    # cv2.imshow("1", img_data.thresholds[0])
    # cv2.imshow("2", img_data.thresholds[1])
    # cv2.imshow("3", img_data.thresholds[2])

    # cv2.imwrite("plate.jpg", img_data.crop_gray)
    # cv2.imwrite("thresh.jpg", img_data.thresholds[0])

    img_data = characteran.characteranalysis(img_data)
    if img_data.disqualified == True:
Example #4
0
def detect():
	cv2.imwrite('input.jpg', event.cv2img)
	detector.detect()
    def run(self):
        classifier.set_mode_gpu()
        # while True:

        start = timeit.default_timer()

        filepath = '/home/ps/22'

        num = 0
        for root, dirs, files in os.walk(filepath):
            for file in files:
                num += 1
                if num % 1000 == 0:
                    print("num=%d" % num)
                filename = file.split('.')[0]
                im = cv2.imread(os.path.join(filepath, file))

                #im = im_[:,50:1780]
                height, width = im.shape[:2]
                cv2.namedWindow('read image', 0)
                cv2.resizeWindow('read image', 640, int(640 * height / width))
                cv2.imshow('read image', im)
                #im = self.read()
                start = timeit.default_timer()

                detections = detector.detect(self.detect_net, im)

                self.coals = []
                im_display = np.copy(im)
                i = 0
                for x_min, y_min, x_max, y_max in detections:
                    i += 1
                    block_crop = im[y_min:y_max, x_min:x_max]
                    self.save_photo(block_crop, filename, i)
                    is_coal = False
                    score = classifier.classify(self.class_net, block_crop)
                    score_str = '%.2f' % score
                    if score > self.one_level_score:
                        if self.two_level:
                            s = classifier.classify(self.class_special_net,
                                                    block_crop)
                            score_str += '_%.2f' % s
                        if not self.two_level or s > self.two_level_score:
                            is_coal = True
                            im_display = cv2.rectangle(im_display,
                                                       (x_min, y_min),
                                                       (x_max, y_max),
                                                       (163, 73, 164), 3)
                            im_display = cv2.putText(im_display,
                                                     'COAL: %.2f' % score,
                                                     (x_min + 5, y_min + 15),
                                                     cv2.FONT_HERSHEY_SIMPLEX,
                                                     1, (255, 0, 0), 2)

                            if self.send_coal:
                                self.coals.append(
                                    (.5 * (x_min + x_max) / width,
                                     .5 * (y_min + y_max) / height,
                                     1. * (x_max - x_min) / width,
                                     1. * (y_max - y_min) / height))
                    if not is_coal:
                        im_display = cv2.rectangle(im_display, (x_min, y_min),
                                                   (x_max, y_max),
                                                   (0, 255, 128), 3)
                        im_display = cv2.putText(im_display,
                                                 'STONE: %.2f' % score,
                                                 (x_min + 5, y_min + 15),
                                                 cv2.FONT_HERSHEY_SIMPLEX, 1,
                                                 (255, 0, 0), 2)
                        if not self.send_coal:
                            self.coals.append((.5 * (x_min + x_max) / width,
                                               .5 * (y_min + y_max) / height,
                                               1. * (x_max - x_min) / width,
                                               1. * (y_max - y_min) / height))

                    #self.save('COAL' if is_coal else 'STONE',  block_crop, score_str)

                    cv2.namedWindow('anchor_box', 0)
                    cv2.resizeWindow('anchor_box', 640,
                                     int(640 * height / width))
                    cv2.imshow('anchor_box', im_display)
                    if cv2.waitKey(1) == 40:
                        break
                self.save_anchor('anchor_box', im_display)
                endlo = timeit.default_timer()
                delay = endlo - start
                print('Delay: %.4fs' % delay)
Example #6
0
    def LK(self,
           path):  # Lucas Kanade Tomasi, detections - just the first frame

        print(cv.TERM_CRITERIA_EPS, cv.TERM_CRITERIA_COUNT)

        ## Video Read
        cap = cv.VideoCapture(path)

        ## Sequence of Images
        #image_list = glob.glob(path+"*.png")
        #image_list.sort()
        #images = []
        #for image in image_list:
        #    image = cv.imread(image,-1)
        #    images.append(image)
        #images = np.array(images)

        # params for ShiTomasi corner detection
        feature_params = dict(maxCorners=100,
                              qualityLevel=0.3,
                              minDistance=7,
                              blockSize=7)
        # Parameters for lucas kanade optical flow
        lk_params = dict(winSize=(15, 15),
                         maxLevel=2,
                         criteria=(cv.TERM_CRITERIA_EPS
                                   | cv.TERM_CRITERIA_COUNT, 10, 0.03))

        # Create some random colors
        color = np.random.randint(0, 255, (100, 3))
        # Take first frame and find corners in it

        #tracker
        tracker = tracker_lk(30, 1000, 30000000, 100)

        # Video
        ret, old_frame = cap.read()
        # Images
        #old_frame = images[0]

        old_gray = cv.cvtColor(old_frame, cv.COLOR_BGR2GRAY)
        detections = detect(old_frame)
        #detections = box_suppression(detections)

        # Video
        while (1):
            #try:
            ret, frame = cap.read()
            if frame is None:
                break
            frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
            tracker.Update(detections, old_gray, frame_gray)
            #print (frame.shape)

            detections = detect(frame)
            #detections = box_suppression(detections)
            detections[:, 0:2] = detections[:, 0:2] + detections[:, 2:] / 2

            # Images
            #for j in range(1,len(images)):
            #frame = images[j]
            #print("Frame")
            for i in range(len(tracker.tracks)):
                x, y = (tracker.tracks[i].trace[-2]).astype("int32")
                w, h = (tracker.dimensions[i]).astype("int32")
                if (w < h and w * h > 2500):
                    #if(w*h > 2000):
                    old_frame = cv.rectangle(old_frame,
                                             (int(x - w / 2), int(y - h / 2)),
                                             (int(x + w / 2), int(y + h / 2)),
                                             (255, 0, 0), 2)
                #print (w,h,w*h)

            cv.imshow('frame', old_frame)
            k = cv.waitKey(30) & 0xff
            if k == 27:
                break
            # Now update the previous frame and previous points
            old_frame = frame.copy()
            old_gray = frame_gray.copy()
            #time.sleep(0.1)
        #except:
        #    k = cv.waitKey(30) & 0xff
        #    if k == 27:
        #        break

        cv.destroyAllWindows()
Example #7
0
def camera():
    output = detector.detect()
Example #8
0
import cv2
import numpy as np
import argparse
from detector import detect

#inisialisasi parameter command line
argpar = argparse.ArgumentParser()
argpar.add_argument("-img",
                    "--input-image",
                    required=True,
                    help="path to input image")
argpar.add_argument("-scale", required=False, type=float, help="output scale")
args = vars(argpar.parse_args())
filename = args["input_image"]

#deteksi citra
img = cv2.imread(filename, 1)
detect(img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example #9
0
#import dataset
#import detector
while True:
    print("1.add user")
    print("2. check user ")
    print("3. exit ")
    n = int(input("enter ur choice"))
    if (n == 1):
        #userdata()
        import dataset
    elif (n == 2):
        #detect()
        import detector
        detector.detect()
    elif n == 3:
        break
    else:
        print("Invalid input")
Example #10
0
def emit(filename: str, *, use_fullname: bool = False) -> None:
    from dictknife import loading
    from prestring.python import Module
    from detector import detect, Object, TypeInfo, ZERO, generate_annotations

    name_map = {}
    m = Module()
    m.toplevel = m.submodule()
    m.sep()

    def _pytype(info: TypeInfo,
                *,
                m=m,
                aliases: t.Dict[str, str] = {"typing": "t"}):
        if info is ZERO:
            module = aliases.get(t.__name__) or t.__name__
            return f"{module}.Any"

        if hasattr(info, "base"):
            module = aliases.get(info.base.__module__) or info.base.__module__
            m.toplevel.import_(info.base.__module__,
                               aliases.get(info.base.__module__))
            if info.base is t.Optional:
                return f"{module}.Optional[{_pytype(info.item)}]"
            elif info.base is t.List:
                return f"{module}.List[{_pytype(info.item)}]"
        elif hasattr(info, "type"):
            module = aliases.get(info.type.__module__) or info.type.__module__
            prefix = module + "."
            if module == "builtins":
                prefix = ""
            else:
                m.toplevel.import_(info.type.__module__,
                                   aliases.get(info.type.__module__))
            return prefix + info.type.__name__
        try:
            return name_map[id(info)]
        except KeyError:
            # FIXME: bug
            import sys

            print(f"something wrong: {info}", file=sys.stderr)
            return "UNKNOWN"

    d = loading.loadfile(filename)
    result = detect(d)
    annotations = generate_annotations(result,
                                       use_fullname=use_fullname,
                                       toplevel_name="toplevel")

    for info in result.history:
        if isinstance(info, Object):
            metadata = annotations["/".join(info.path)]
            name = metadata.get("after", metadata["before"])["name"]
            name_map[id(info)] = name

            m.stmt(f"# from: {'/'.join(info.path)}")
            with m.class_(name):
                for name, sub_info in info.props.items():
                    m.stmt("{}: {}", name, _pytype(sub_info))
    print(m)
import cv2
import numpy as np
import argparse
from detector import detect
import os
import glob

#inisialisasi parameter command line
argpar=argparse.ArgumentParser()
argpar.add_argument("-img", required=True, help="path to input image")
argpar.add_argument("-out",  action='store_true', help="path to output image")
argpar.add_argument("-scale", required=False, type=float, help="output scale")
args=vars(argpar.parse_args())
filename=glob.glob(pathname=args["img"]+str("/*"))

#untuk setiap file yang ditemukan di folder, lakukan operasi pendeteksian
for f in filename:
    img=cv2.imread(f, 1)
    result, smileCount=detect(img)
    if(args["out"]):
        cv2.imwrite("output/out"+str(os.path.basename(f)), img)
cv2.destroyAllWindows()
Example #12
0
def validate_one(json_file, image_file):
    def match_ele_and_shape(e_index, e_rec, e_type, shapes_list, shapes_label,
                            fake_elements, image_ele_id_map):
        ele_area = e_rec[2] * e_rec[3]

        matched = False
        for shape_id in shapes_list:
            # [element_type, shape_bound, element_id]
            ele_label = shapes_label[shape_id]
            shape_rec = ele_label[1]
            shape_area = shape_rec[2] * shape_rec[3]
            overlap_area = helper.get_overlap_area(e_rec, shape_rec)
            if overlap_area / ele_area > 0.5 and overlap_area / shape_area > 0.5:
                # print("here")
                shapes_list.remove(shape_id)
                image_ele_id_map[e_index] = ele_label[-1]

                ele_shape_type = get_type_info(ele_label[0])
                # print(ele_shape_type)
                matched = True
                # detected_num, type_right, type_wrong
                shape_type_res = shapes_result[ele_shape_type].get(
                    "detect", [0, 0, 0])
                shape_type_res[0] += 1
                if e_type == ele_shape_type:
                    shape_type_res[1] += 1
                else:
                    if e_type == "exclusiveGateway_fork" and ele_shape_type == "exclusiveGateway":
                        shape_type_res[1] += 1
                    else:
                        shape_type_res[2] += 1
                shapes_result[ele_shape_type]["detect"] = shape_type_res
                break

        if not matched:
            image_ele_id_map[e_index] = ""
            fake_elements.append(e_index)
        return shapes_list

    _, all_elements_info, all_seq_flows, all_elements, pools = detector.detect(
        image_file, classifier, classifier_type)

    shapes_result = defaultdict(dict)
    fake_shapes = []
    fake_lanes = []
    fake_pools = []

    with open(json_file, encoding="utf-8", mode='r') as f:
        project_labels = json.load(f)

    pool_labels = project_labels["pool_labels"]
    shape_labels = project_labels["shape_labels"]
    flow_labels = project_labels["flow_labels"]

    base_point = None
    if img_type == "png":
        base_point = [6, 6]
    elif img_type == "jpg":
        base_point = [30, 30]
    else:
        print("invalid img_type")
        exit(0)

    x_min = project_labels["x_min"]
    y_min = project_labels["y_min"]
    x_offset = base_point[0] - x_min
    y_offset = base_point[1] - y_min

    flow_shapes = []
    sub_p_shapes = []
    lane_shapes = []
    pool_shapes = []
    # [element_type, shape_bound, element_id]
    for shape_index, shape_label in enumerate(shape_labels):
        shape_type = get_type_info(shape_label[0])
        shape_rect = shape_label[1]
        shape_rect[0] += x_offset
        shape_rect[1] += y_offset
        shape_label[1] = shape_rect

        if shape_type in labels:
            type_num = shapes_result[shape_type].get("total", 0)
            type_num += 1
            shapes_result[shape_type]["total"] = type_num
            flow_shapes.append(shape_index)
        else:
            if "expanded" in shape_type.split("_"):
                shape_labels[shape_index][0] = "subProcess_expanded"
                type_num = shapes_result["subProcess_expanded"].get("total", 0)
                type_num += 1
                shapes_result["subProcess_expanded"]["total"] = type_num
                sub_p_shapes.append(shape_index)

            if shape_type == "lane":
                type_num = shapes_result["lane"].get("total", 0)
                type_num += 1
                shapes_result["lane"]["total"] = type_num
                lane_shapes.append(shape_index)

    for pool_index, pool_label in enumerate(pool_labels):
        pool_rect = pool_label[1]
        pool_rect[0] += x_offset
        pool_rect[1] += y_offset
        pool_label[1] = pool_rect

        type_num = shapes_result["pool"].get("total", 0)
        type_num += 1
        shapes_result["pool"]["total"] = type_num
        pool_shapes.append(pool_index)

    # {detected element id: bpmn file element id}
    image_shape_id_map = dict()
    image_pool_id_map = dict()
    image_lane_id_map = dict()

    for ele_index, ele_path in enumerate(all_elements):
        ele_rec = get_element_rec_by_path(ele_path, pools)
        ele_type = all_elements_info[ele_index][0]

        if ele_path[-1] == 0:
            # flow elements
            flow_shapes = match_ele_and_shape(ele_index, ele_rec, ele_type,
                                              flow_shapes, shape_labels,
                                              fake_shapes, image_shape_id_map)
        else:
            # expanded sub processes
            sub_p_shapes = match_ele_and_shape(ele_index, ele_rec, ele_type,
                                               sub_p_shapes, shape_labels,
                                               fake_shapes, image_shape_id_map)

    if len(pool_labels) > 0:
        for pool_id, pool in enumerate(pools):
            pool_rect = pool["rect"]

            pool_shapes = match_ele_and_shape(pool_id, pool_rect, "pool",
                                              pool_shapes, pool_labels,
                                              fake_pools, image_pool_id_map)

            lanes = pool["lanes"]
            for lane_id, lane in enumerate(lanes):
                lane_shapes = match_ele_and_shape(
                    (pool_id, lane_id), lane, "lane", lane_shapes,
                    shape_labels, fake_lanes, image_lane_id_map)

    # [file seq num, not matched, target match, source match, others]
    seq_result = [0, 0, 0, 0, 0]

    flows_label_rest = []

    for flow_id in range(len(flow_labels)):
        if flow_labels[flow_id][1] == "sequenceFlow":
            flows_label_rest.append(flow_id)

    seq_result[0] = len(flows_label_rest)

    for seq_flow in all_seq_flows:
        target_ele_id = seq_flow[0]
        source_ele_id = seq_flow[-1]
        try:
            target_ele_ref = image_shape_id_map[target_ele_id]
            source_ele_ref = image_shape_id_map[source_ele_id]
        except KeyError:
            seq_result[-1] += 1
            continue
        # try:
        #     source_ele_ref = image_ele_id_map[source_ele_id]
        # except KeyError:
        #     seq_result[-1] += 1
        #     continue

        same_target_seqs = []
        for flow_id in flows_label_rest:
            # [file_id, main_type, points_label, element_id, source_ref, target_ref]
            flow_label = flow_labels[flow_id]
            target_ref = flow_label[-1]
            source_ref = flow_label[-2]

            if target_ele_ref == target_ref:
                same_target_seqs.append([flow_id, source_ref])

        # matched = False
        if len(same_target_seqs) > 0:
            seq_result[2] += 1
            for same_target_seq in same_target_seqs:
                if source_ele_ref == same_target_seq[1]:
                    seq_result[3] += 1
                    flows_label_rest.remove(same_target_seq[0])
                    # matched = True
                    break

        else:
            seq_result[-1] += 1

    seq_result[1] = len(flows_label_rest)

    interested_labels = labels.copy()
    interested_labels.extend(["lane", "pool", "subProcess_expanded"])

    one_res = {}
    file_name = image_file.split("/")[-1]
    file_label = file_name.split(".")[0]
    one_res["file_label"] = file_label
    # print(image_file)
    one_res["shapes"] = {}
    for label in interested_labels:
        label_result = shapes_result[label]
        # print(label)
        # print(len(label_result))
        if len(label_result) > 0:
            # print(label)
            total = label_result["total"]
            if len(label_result) == 2:
                detect = label_result["detect"]
            else:
                print("not detected")
                detect = [0, 0, 0]
            # print("{}\t{},{},{},{}".format(label, total, detect[0], detect[1], detect[2]))
            one_res["shapes"][label] = [total, detect[0], detect[1], detect[2]]

    # print("fake_elements\t{}".format(len(fake_elements)))
    one_res["fake_shapes"] = len(fake_shapes)
    one_res["fake_pools"] = len(fake_pools)
    one_res["fake_lanes"] = len(fake_lanes)

    seq_record = "sequenceFlow\t{},{},{},{},{}".format(seq_result[0],
                                                       seq_result[1],
                                                       seq_result[2],
                                                       seq_result[3],
                                                       seq_result[4])
    one_res["seq_record"] = seq_record
    # print(seq_record)

    # print("=" * 100)
    return one_res
Example #13
0
def validate(data_dir):
    print("validate {}".format(classifier_type))
    # validate_res_dir = "validate_results_2/"

    projects = os.listdir(validate_data_dir)
    projects.sort()

    # results = []
    for i in range(begin, end):
        project = projects[i]
        if project in invalid_projects:
            continue
        print("----------------{}, {}----------------".format(i, project))
        project_dir = "{}{}".format(validate_data_dir, project)
        files = os.listdir(project_dir)
        png_file = ""
        jpg_file = ""
        json_file = ""
        for file in files:
            if file.endswith("png"):
                png_file = "{}/{}".format(project_dir, file)
            if file.endswith("jpeg"):
                jpg_file = "{}/{}".format(project_dir, file)
            if file.endswith("json"):
                json_file = "{}/{}".format(project_dir, file)

        if img_type == "png":
            image_file = png_file
        else:
            image_file = jpg_file

        try:
            _, all_elements_info, all_seq_flows, all_elements, pools, time_recorder = detector.detect(
                image_file, classifier, classifier_type)

        except TypeError:
            print("invalid!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  TypeError")
            continue
        except IndexError:
            print("invalid!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  IndexError")
            continue
        for seq_flow in all_seq_flows:
            points = seq_flow[1]
            for p_id in range(len(points)):
                points[p_id] = [float(x) for x in points[p_id]]
        # print(all_seq_flows)
        detect_result = {
            "all_elements_info": all_elements_info,
            "all_seq_flows": all_seq_flows,
            "all_elements": all_elements,
            "pools": pools,
            "time_recorder": time_recorder
        }

        result_root_dir = "detect_results_all_gpu"
        image_type_result_dir = "{}/{}".format(result_root_dir, img_type)

        project_result_dir = "{}/{}_{}".format(image_type_result_dir, project,
                                               i)

        if not os.path.exists(project_result_dir):
            os.makedirs(project_result_dir, exist_ok=True)

        result_file = "{}/{}_detect.json".format(project_result_dir, project)
        # temp = json.dumps(detect_result)
        # print(result_file)
        with open(result_file, encoding="utf-8", mode="w") as f:
            json.dump(detect_result, f)