Example #1
0
def ocr_lp(output_dir, loaded_models, Iorig_path):

    Iorig_name = basename(splitext(Iorig_path)[0])
    imgs_paths = sorted(glob('%s/%s_*_lp.png' % (output_dir, Iorig_name)))

    Iorig = cv2.imread(Iorig_path)

    for i, img_path in enumerate(imgs_paths):

        bname = basename(splitext(img_path)[0])

        ocr_net, ocr_meta, ocr_threshold = loaded_models[2]

        R, (width, height) = detect(ocr_net,
                                    ocr_meta,
                                    img_path.encode('utf-8'),
                                    thresh=ocr_threshold,
                                    nms=None)

        if len(R):

            L = dknet_label_conversion(R, width, height)
            L = nms(L, .45)

            L.sort(key=lambda x: x.tl()[0])
            lp_str = ''.join([chr(l.cl()) for l in L])

            with open('%s/%s_str.txt' % (output_dir, bname), 'w') as f:
                f.write(lp_str + '\n')

            print('\t\tLP: %s' % lp_str)

        else:

            print('No characters found')
Example #2
0
def OCRDection(LPImagePath):
    # OCR recognition
    W, (width, height) = detect(ocr_net,
                                ocr_meta,
                                LPImagePath,
                                thresh=ocr_threshold,
                                nms=None)
    if len(W):
        L = dknet_label_conversion(W, width, height)
        L = nms(L, .45)
        L.sort(key=lambda x: x.tl()[0])
        lp_str = ''.join([chr(l.cl()) for l in L])

        # plates always upper case, do not contain "I", "O" or "Q"
        lp_str = lp_str.upper()
        lp_str = lp_str.replace("I", "1")
        lp_str = lp_str.replace("O", "0")
        lp_str = lp_str.replace("Q", "0")

        bname = basename(splitext(LPImagePath)[0])
        dname = dirname(LPImagePath)
        LPTextPath = '%s/%s_str.txt' % (dname, bname)

        with open(LPTextPath, 'w') as f:
            f.write(lp_str + '\n')
        return lp_str
    else:
        return ""
    def detect_lp(self, img, filename, i):
        ratio = float(max(img.shape[:2])) / min(img.shape[:2])
        side = int(ratio*288.)
        bound_dim = min(side + (side % (2**4)), 608)

        Llp, LlpImgs, _ = detect_lp(self.wpod_net, im2single(img), bound_dim, 2**4, (240, 80), self.lp_threshold)
        if len(LlpImgs):
            Ilp = LlpImgs[0]
            Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY)
            Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR)

            s = Shape(Llp[0].pts)

            cv2.imwrite('%s/%s-lp-%s.png' % (self.output_dir, filename, i), Ilp*255.)

            # LP OCR
            R, idxs, boxes, confidences, class_ids = self.lp.detect_objects(Ilp*255., filename)

            print('\t\t%d lps found' % len(R))
            
            if len(R):
                L = dknet_label_conversion(R, 240, 80)
                L = nms(L, .45)
                L.sort(key=lambda x: x.tl()[0])
                lp_str = ''.join([chr(l.cl()) for l in L])
                if (len(lp_str) <= 3):
                    return "No license found"
                return lp_str
        
        return "No license found"
Example #4
0
def ocr_detect(ocr_net, ocr_meta, Ilps):
    print ('Performing OCR...')
    lp_strs = []
    for i, Ilp in enumerate(Ilps):
        if (Ilp is not np.nan):
            print ('\tScanning car %s' % i)
            R,(width,height) = detect2(ocr_net, ocr_meta, Ilp, thresh=ocr_threshold, nms=None)
            if len(R):
                L = dknet_label_conversion(R,width,height)
                L = nms(L,.45)
                L.sort(key=lambda x: x.tl()[0])
                lp_str = ''.join([chr(l.cl()) for l in L])
                sys.stdout.write('\t\t%s\n' % lp_str)
                regex = re.compile(r'([0-9|I]{4}[A-Z][A-Z|0-9])|([A-Z]{3}[0-9|I]{4})')
                match = regex.search(lp_str)
                sys.stdout.write('\t\t%s\n' % match)
                if ((len(lp_str)==6 or len(lp_str)==7) and match is not None):
                    lp_strs.append(lp_str)
                else:
                    lp_strs.append(np.nan)         
            else:
                lp_strs.append(np.nan)
        else:
            lp_strs.append(np.nan)
    return lp_strs
 def text_extract(img_path, height, width):
     R = detect(ocr_net, ocr_meta, img_path, thresh=0.4, nms=None)
     if len(R):
         L = dknet_label_conversion(R, width, height)
         L = nms(L, .45)
         L.sort(key=lambda x: x.tl()[0])
         lp_str1 = ''.join([chr(l.cl()) for l in L])
     print("License plate ----", lp_str1)
def lp_ocr(input_dir):

    output_dir = input_dir

    try:
        ocr_threshold = .4

        ocr_weights = 'data/ocr/ocr-net.weights'
        ocr_netcfg = 'data/ocr/ocr-net.cfg'
        ocr_dataset = 'data/ocr/ocr-net.data'

        ocr_net = dn.load_net(ocr_netcfg, ocr_weights, 0)
        ocr_meta = dn.load_meta(ocr_dataset)

        imgs_paths = sorted(glob('%s/*lp.png' % output_dir))

        print 'Performing OCR...'

        for i, img_path in enumerate(imgs_paths):

            print '\tScanning %s' % img_path

            bname = basename(splitext(img_path)[0])

            R, (width, height) = detect(ocr_net,
                                        ocr_meta,
                                        img_path,
                                        thresh=ocr_threshold,
                                        nms=None)

            if len(R):

                L = dknet_label_conversion(R, width, height)
                L = nms(L, .45)

                L.sort(key=lambda x: x.tl()[0])
                lp_str = ''.join([chr(l.cl()) for l in L])

                # print "AFTER NMS AND SORT"
                # for label in L:
                #     print label.letter(), label.prob(), label
                # print "=========================================================================="

                prob_str = ''.join([str(l.prob()) + ',' for l in L])

                with open('%s/%s_str.txt' % (output_dir, bname), 'w') as f:
                    f.write(lp_str + '\n')
                    f.write(prob_str + '\n')

                print '\t\tLP: %s' % lp_str

            else:

                print 'No characters found'

    except:
        traceback.print_exc()
        sys.exit(1)
    def text_extract(img_path, height, width):
        '''gray = cv2.cvtColor(img_path, cv2.COLOR_BGR2GRAY)

        gray = cv2.threshold(
            gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        inv = 255 - gray
        horizontal_img = inv
        vertical_img = inv

        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (100, 1))
        horizontal_img = cv2.erode(horizontal_img, kernel, iterations=1)
        horizontal_img = cv2.dilate(horizontal_img, kernel, iterations=1)
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 100))
        vertical_img = cv2.erode(vertical_img, kernel, iterations=1)
        vertical_img = cv2.dilate(vertical_img, kernel, iterations=1)

        mask_img = horizontal_img + vertical_img

        no_border = np.bitwise_or(gray, mask_img)
        os.system("convert " + " no.jpg" + " -bordercolor " +
                  " White" + " -border" + " 10x10" + " with_border.jpg")
        imagez = cv2.imread("with_border.jpg")
        p1 = (20, 30)
        p2 = (60, 80)
        p3 = (60, 15)
        p4 = (200, 45)
        (x1, y1) = (20, 30)
        (x2, y2) = (60, 80)
        (x3, y3) = (60, 15)
        (x4, y4) = (200, 45)

        color = (255, 0, 0)
        thickness = 2
        image1 = cv2.rectangle(imagez, p1, p2, color, thickness)
        image2 = cv2.rectangle(imagez, p3, p4, color, thickness)

        roi1 = image1[y1:y2, x1:x2]

        roi2 = image2[y3:y4, x3:x4]'''

        ocr_threshold = .4
        R = detect(
            ocr_net, ocr_meta, img_path, thresh=ocr_threshold, nms=None)
        if len(R):
            L = dknet_label_conversion(R, width, height)
            L = nms(L, .45)
            L.sort(key=lambda x: x.tl()[0])
            lp_str1 = ''.join([chr(l.cl()) for l in L])
        print("License plate ----",lp_str1)
Example #8
0
def lp_ocr_one_img(img_path, ocr_dn_net, ocr_dn_meta, ocr_thd):
    print '\tScanning %s' % img_path,
    st = time.time()
    R, (width, height) = dn.detect(ocr_dn_net,
                                   ocr_dn_meta,
                                   img_path,
                                   thresh=ocr_thd,
                                   nms=None)
    lp_str = ""
    if len(R):
        L = dknet_label_conversion(R, width, height)
        L = nms(L, .45)
        L.sort(key=lambda x: x.tl()[0])
        lp_str = ''.join([chr(l.cl()) for l in L])
    print 'runtime: %.1f' % (time.time() - st)
    return lp_str
    def plates_ocr(self,img):
        # imgs_paths = sorted(glob('%s/*.png' % self.output_dir))
        # print(imgs_paths)

        #print('Performing OCR...')

        # for i, img_path in enumerate(imgs_paths):
        #     print("Frame {} out of {}".format(i, len(imgs_paths)))
        #     print('\tScanning %s' % img_path)

        # bname=basename(splitext(img_path)[0])
        # print(bname)
        # img= cv2.imread(img_path)
        height, width= img.shape[:2]
        
        R=detect(self.ocr_net, self.ocr_meta,
            img, thresh=self.ocr_threshold, nms=.45)
        #print(R)
        if len(R):

            L=dknet_label_conversion(R, width, height)
            L=nms(L, .45)

            L.sort(key=lambda x: x.tl()[0])
            lp_str=''.join([chr(l.cl()) for l in L])

            # with open('%s/%s_str.txt' % (self.output_dir, bname), 'w') as f:
            #     f.write(lp_str + '\n')

            # print '\t\tLP: %s' % lp_str
            return lp_str

        else:

            # print 'No characters found'
            return 'No characters found'
        for i, img_path in enumerate(imgs_paths):

            print('\tScanning %s' % img_path)

            bname = basename(splitext(img_path)[0])

            R, (width, height) = detect(ocr_net,
                                        ocr_meta,
                                        img_path.encode('utf-8'),
                                        thresh=ocr_threshold,
                                        nms=None)

            if len(R):

                L = dknet_label_conversion(R, width, height)
                L = nms(L, .45)

                L.sort(key=lambda x: x.tl()[0])
                lp_str = ''.join([chr(l.cl()) for l in L])

                with open('%s/%s_str.txt' % (output_dir, bname), 'w') as f:
                    f.write(lp_str + '\n')

                print('\t\tLP: %s' % lp_str)
                file1.write(lp_str)

            else:

                print('No characters found')
        file1.write("\n")
Example #11
0
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print("Received created event - %s." % event.src_path)
            try:
                img_path = event.src_path

                print('\tScanning %s' % img_path)

                bname = basename(splitext(img_path)[0])

                R, (width, height) = detect(ocr_net,
                                            ocr_meta,
                                            img_path,
                                            thresh=ocr_threshold,
                                            nms=None)

                if len(R):

                    L = dknet_label_conversion(R, width, height)
                    L = nms(L, .45)

                    L.sort(key=lambda x: x.tl()[0])
                    lp_str = ''.join([chr(l.cl()) for l in L])

                    # with open('%s/%s_str.txt' % (output_dir, bname), 'w') as f:
                    #         f.write(lp_str + '\n')
                    # change line below if you'd like a range (6 OR 7 character LPs, however false readings increase dramatically)
                    if 7 <= len(lp_str) <= 7:

                        print('\t\tLP: %s' % lp_str)
                        # get the timestamp based off of the frameImg filename
                        file_name = os.path.basename(
                            event.src_path).split('.')[0]
                        video_date = file_name.split('_')[0]
                        print(video_date)
                        camera_location = file_name.split('-')[-2]
                        print(camera_location)
                        # pre_video_time = file_name.split('_')[1]
                        video_time = '-'.join(
                            file_name.split('_')[1].split('-')[0:3])
                        print(video_time)
                        datetime_string = '{} {}'.format(
                            video_date, video_time)
                        print(datetime_string)
                        video_start_time = datetime.datetime.strptime(
                            datetime_string, '%Y-%m-%d %H-%M-%S').timestamp()
                        print(video_start_time)
                        frame_time_seconds = (round(
                            int(file_name.split('-')[-1].split('_')[0]) / 4) -
                                              1)
                        if frame_time_seconds < 0:
                            frame_time_seconds = 0
                        print(frame_time_seconds)
                        tz = pytz.timezone('America/Los_Angeles')
                        start_time_seconds = datetime.datetime.fromtimestamp(
                            video_start_time, tz)
                        print(start_time_seconds)
                        print(int(start_time_seconds.strftime("%s")))
                        detection_time = (
                            int(start_time_seconds.strftime("%s")) +
                            frame_time_seconds)
                        print(detection_time)

                        matching_poll = db.polls.find_one(
                            {"unixTS": detection_time})
                        if matching_poll:
                            print(matching_poll)

                            # does the poll have a geocodeID?
                            ####geocode temp fix, for testing. everything in prod will have a street value
                            reqHeaders = {'User-Agent': 'SDScout/0.0.1'}
                            reqGeoCode = requests.get(
                                url=
                                'https://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1'
                                .format(
                                    matching_poll['location']['coordinates']
                                    [1], matching_poll['location']
                                    ['coordinates'][0]),
                                headers=reqHeaders)
                            print(reqGeoCode.json())
                            geoObject = reqGeoCode.json()
                            # reqGeoCode.json()[0]['year']

                            # now save geocode as it's own document for forward compatibility
                            new_geocode = {
                                'place_id':
                                geoObject['place_id'],
                                'osm_type':
                                geoObject['osm_type'],
                                'osm_id':
                                geoObject['osm_id'],
                                'location': {
                                    'type':
                                    "Point",
                                    'coordinates':
                                    [geoObject['lon'], geoObject['lat']]
                                },
                                'display_name':
                                geoObject['display_name'],
                                'road':
                                geoObject['address']['road'],
                                'city':
                                geoObject['address']['city'],
                                'county':
                                geoObject['address']['county'],
                                'state':
                                geoObject['address']['state'],
                                'postcode':
                                geoObject['address']['postcode'],
                                'country':
                                geoObject['address']['country'],
                                'country_code':
                                geoObject['address']['country_code'],
                                'boundingbox':
                                geoObject['boundingbox']
                            }
                            geocode_id = db.geocodes.insert_one(
                                new_geocode).inserted_id

                            # update the matching poll doc
                            db.Doc.update({"_id": matching_poll["_id"]}, {
                                "$set": {
                                    'street': geoObject['address']['road'],
                                    'city': geoObject['address']['city'],
                                    'geocodeID': geocode_id,
                                }
                            })
                            ####end temp geocode fix

                            existing_plate = db.plates.find_one(
                                {"plateContent": lp_str})

                            if existing_plate:
                                print('plate exists!', existing_plate)
                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'driveID':
                                    matching_poll['driveID'] or undefined,
                                    'speed':
                                    matching_poll['speed'],
                                    'street':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'geocodeID':
                                    geocode_id,
                                    'power':
                                    matching_poll['power'],
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    existing_plate['_id'],
                                    'vyear':
                                    existing_plate['vyear'],
                                    'vmake':
                                    existing_plate['vmake'],
                                    'vmodel':
                                    existing_plate['vmodel'],
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/Videos/%s.mp4' % ('-'.join(
                                        '_'.join(file_name.split('_')
                                                 [:-2]).split('-')[:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One plate detection added: {0}'.format(
                                    result.inserted_id))

                            else:
                                # we need to create a doc in PLATES first, get that objectID, then add the plateDetection

                                # but first we need to do the make model lookup
                                session = requests.Session()
                                session.headers.update({
                                    'User-Agent':
                                    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0'
                                })
                                reqMakeModel = session.get(
                                    'https://www.carfax.com/api/mobile-homepage-quickvin-check?plate={}&state={}'
                                    .format(lp_str, homestate))

                                newVyear = '-'
                                newVmake = '-'
                                newVmodel = '-'
                                print(reqMakeModel.json())

                                try:
                                    newVyear = reqMakeModel.json(
                                    )['quickVinResults'][0]['year']
                                    newVmake = reqMakeModel.json(
                                    )['quickVinResults'][0]['make']
                                    newVmodel = reqMakeModel.json(
                                    )['quickVinResults'][0]['model']
                                except:
                                    print('plate not found')

                                new_plate = {
                                    'plateContent': lp_str,
                                    'vyear': newVyear,
                                    'vmake': newVmake,
                                    'vmodel': newVmodel
                                }
                                plate_id = db.plates.insert_one(
                                    new_plate).inserted_id

                                # dont forget to add geocode

                                ####geocode temp fix, for testing. everything in prod will have a street value
                                # this is a parked detection so we need to get geocode
                                reqHeaders = {'User-Agent': 'SDScout/0.0.1'}
                                reqGeoCode = requests.get(
                                    url=
                                    'https://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1'
                                    .format(
                                        matching_poll['location']
                                        ['coordinates'][1],
                                        matching_poll['location']
                                        ['coordinates'][0]),
                                    headers=reqHeaders)
                                print(reqGeoCode.json())
                                geoObject = reqGeoCode.json()
                                # reqGeoCode.json()[0]['year']

                                # now save geocode as it's own document for forward compatibility
                                new_geocode = {
                                    'place_id':
                                    geoObject['place_id'],
                                    'osm_type':
                                    geoObject['osm_type'],
                                    'osm_id':
                                    geoObject['osm_id'],
                                    'location': {
                                        'type':
                                        "Point",
                                        'coordinates':
                                        [geoObject['lon'], geoObject['lat']]
                                    },
                                    'display_name':
                                    geoObject['display_name'],
                                    'road':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'county':
                                    geoObject['address']['county'],
                                    'state':
                                    geoObject['address']['state'],
                                    'postcode':
                                    geoObject['address']['postcode'],
                                    'country':
                                    geoObject['address']['country'],
                                    'country_code':
                                    geoObject['address']['country_code'],
                                    'boundingbox':
                                    geoObject['boundingbox']
                                }
                                geocode_id = db.geocodes.insert_one(
                                    new_geocode).inserted_id

                                # now write the actual detection doc
                                print(
                                    'for reference, the filename of the video is '
                                )

                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'driveID':
                                    matching_poll['driveID'] or undefined,
                                    'speed':
                                    matching_poll['speed'],
                                    'power':
                                    matching_poll['power'],
                                    'vyear':
                                    newVyear,
                                    'vmake':
                                    newVmake,
                                    'vmodel':
                                    newVmodel,
                                    'street':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'geocodeID':
                                    geocode_id,
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    plate_id,
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/Videos/%s.mp4' % ('-'.join(
                                        '_'.join(file_name.split('_')
                                                 [:-2]).split('-')[:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One detection added: {0}'.format(
                                    result.inserted_id))

                                # update the matching poll doc
                                db.Doc.update({"_id": matching_poll["_id"]}, {
                                    "$set": {
                                        'street': geoObject['address']['road'],
                                        'city': geoObject['address']['city'],
                                        'geocodeID': geocode_id,
                                    }
                                })
                        else:
                            print(
                                "no matching poll found, entering a fake coordinate and marking to review later"
                            )
                            matching_poll = db.polls.find_one(
                                {"_id": ObjectId('5dabb930657f9d3e10178866')})
                            print(matching_poll)

                            # does the poll have a geocodeID?
                            ####geocode temp fix, for testing. everything in prod will have a street value
                            reqHeaders = {'User-Agent': 'SDScout/0.0.1'}
                            reqGeoCode = requests.get(
                                url=
                                'https://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1'
                                .format(
                                    matching_poll['location']['coordinates']
                                    [1], matching_poll['location']
                                    ['coordinates'][0]),
                                headers=reqHeaders)
                            print(reqGeoCode.json())
                            geoObject = reqGeoCode.json()
                            # reqGeoCode.json()[0]['year']

                            # now save geocode as it's own document for forward compatibility
                            new_geocode = {
                                'place_id':
                                geoObject['place_id'],
                                'osm_type':
                                geoObject['osm_type'],
                                'osm_id':
                                geoObject['osm_id'],
                                'location': {
                                    'type':
                                    "Point",
                                    'coordinates':
                                    [geoObject['lon'], geoObject['lat']]
                                },
                                'display_name':
                                geoObject['display_name'],
                                'road':
                                geoObject['address']['road'],
                                'city':
                                geoObject['address']['city'],
                                'county':
                                geoObject['address']['county'],
                                'state':
                                geoObject['address']['state'],
                                'postcode':
                                geoObject['address']['postcode'],
                                'country':
                                geoObject['address']['country'],
                                'country_code':
                                geoObject['address']['country_code'],
                                'boundingbox':
                                geoObject['boundingbox']
                            }
                            geocode_id = db.geocodes.insert_one(
                                new_geocode).inserted_id

                            # update the matching poll doc
                            db.Doc.update({"_id": matching_poll["_id"]}, {
                                "$set": {
                                    'street': geoObject['address']['road'],
                                    'city': geoObject['address']['city'],
                                    'geocodeID': geocode_id,
                                }
                            })
                            ####end temp geocode fix

                            existing_plate = db.plates.find_one(
                                {"plateContent": lp_str})

                            if existing_plate:
                                print('plate exists!', existing_plate)
                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'fakePoll':
                                    True,
                                    'driveID':
                                    ObjectId('5d3f848be08cfb4a873092db'),
                                    'speed':
                                    matching_poll['speed'],
                                    'street':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'geocodeID':
                                    geocode_id,
                                    'power':
                                    matching_poll['power'],
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    existing_plate['_id'],
                                    'vyear':
                                    existing_plate['vyear'],
                                    'vmake':
                                    existing_plate['vmake'],
                                    'vmodel':
                                    existing_plate['vmodel'],
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/Videos/%s.mp4' % ('-'.join(
                                        '_'.join(file_name.split('_')
                                                 [:-2]).split('-')[:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One plate detection added: {0}'.format(
                                    result.inserted_id))

                            else:
                                # we need to create a doc in PLATES first, get that objectID, then add the plateDetection

                                # but first we need to do the make model lookup
                                session = requests.Session()
                                session.headers.update({
                                    'User-Agent':
                                    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0'
                                })
                                reqMakeModel = session.get(
                                    'https://www.carfax.com/api/mobile-homepage-quickvin-check?plate={}&state={}'
                                    .format(lp_str, homestate))

                                newVyear = '-'
                                newVmake = '-'
                                newVmodel = '-'
                                print(reqMakeModel.json())

                                try:
                                    newVyear = reqMakeModel.json(
                                    )['quickVinResults'][0]['year']
                                    newVmake = reqMakeModel.json(
                                    )['quickVinResults'][0]['make']
                                    newVmodel = reqMakeModel.json(
                                    )['quickVinResults'][0]['model']
                                except:
                                    print('plate not found')

                                new_plate = {
                                    'plateContent': lp_str,
                                    'vyear': newVyear,
                                    'vmake': newVmake,
                                    'vmodel': newVmodel
                                }
                                plate_id = db.plates.insert_one(
                                    new_plate).inserted_id

                                # dont forget to add geocode

                                ####geocode temp fix, for testing. everything in prod will have a street value
                                # this is a parked detection so we need to get geocode
                                reqHeaders = {'User-Agent': 'SDScout/0.0.1'}
                                reqGeoCode = requests.get(
                                    url=
                                    'https://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}&zoom=18&addressdetails=1'
                                    .format(
                                        matching_poll['location']
                                        ['coordinates'][1],
                                        matching_poll['location']
                                        ['coordinates'][0]),
                                    headers=reqHeaders)
                                print(reqGeoCode.json())
                                geoObject = reqGeoCode.json()
                                # reqGeoCode.json()[0]['year']

                                # now save geocode as it's own document for forward compatibility
                                new_geocode = {
                                    'place_id':
                                    geoObject['place_id'],
                                    'osm_type':
                                    geoObject['osm_type'],
                                    'osm_id':
                                    geoObject['osm_id'],
                                    'location': {
                                        'type':
                                        "Point",
                                        'coordinates':
                                        [geoObject['lon'], geoObject['lat']]
                                    },
                                    'display_name':
                                    geoObject['display_name'],
                                    'road':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'county':
                                    geoObject['address']['county'],
                                    'state':
                                    geoObject['address']['state'],
                                    'postcode':
                                    geoObject['address']['postcode'],
                                    'country':
                                    geoObject['address']['country'],
                                    'country_code':
                                    geoObject['address']['country_code'],
                                    'boundingbox':
                                    geoObject['boundingbox']
                                }
                                geocode_id = db.geocodes.insert_one(
                                    new_geocode).inserted_id

                                # now write the actual detection doc
                                print(
                                    'for reference, the filename of the video is '
                                )

                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'driveID':
                                    ObjectId('5d3f848be08cfb4a873092db'),
                                    'speed':
                                    matching_poll['speed'],
                                    'power':
                                    matching_poll['power'],
                                    'vyear':
                                    newVyear,
                                    'vmake':
                                    newVmake,
                                    'vmodel':
                                    newVmodel,
                                    'street':
                                    geoObject['address']['road'],
                                    'city':
                                    geoObject['address']['city'],
                                    'geocodeID':
                                    geocode_id,
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    plate_id,
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/Videos/%s.mp4' % ('-'.join(
                                        '_'.join(file_name.split('_')
                                                 [:-2]).split('-')[:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One detection added: {0}'.format(
                                    result.inserted_id))

                                # update the matching poll doc
                                db.Doc.update({"_id": matching_poll["_id"]}, {
                                    "$set": {
                                        'street': geoObject['address']['road'],
                                        'city': geoObject['address']['city'],
                                        'geocodeID': geocode_id,
                                    }
                                })

                    else:
                        print(
                            'plate detected too short, likely false positive: ',
                            lp_str)
                        if os.path.exists(img_path):
                            os.remove(img_path)
                        # remove vehicle image as well
                        vehicleImgPath = img_path.rsplit('_', 1)[0].replace(
                            "plates", "vehicles") + '.png'
                        if os.path.exists(vehicleImgPath):
                            os.remove(vehicleImgPath)
                        print('removing {} and {}'.format(
                            img_path, vehicleImgPath))

                else:
                    print('No characters found')
                    if os.path.exists(img_path):
                        os.remove(img_path)
                    # remove vehicle image as well
                    vehicleImgPath = img_path.rsplit('_', 1)[0].replace(
                        "plates", "vehicles") + '.png'
                    if os.path.exists(img_path):
                        os.remove(vehicleImgPath)
                    print('removing {} and {}'.format(img_path,
                                                      vehicleImgPath))

            except:
                traceback.print_exc()
                sys.exit(1)

        elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            print("Received modified event - %s." % event.src_path)
Example #12
0
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print("Received created event - %s." % event.src_path)
            try:
                img_path = event.src_path

                print('\tScanning %s' % img_path)

                bname = basename(splitext(img_path)[0])

                R, (width, height) = detect(ocr_net,
                                            ocr_meta,
                                            img_path,
                                            thresh=ocr_threshold,
                                            nms=None)

                if len(R):

                    L = dknet_label_conversion(R, width, height)
                    L = nms(L, .45)

                    L.sort(key=lambda x: x.tl()[0])
                    lp_str = ''.join([chr(l.cl()) for l in L])

                    # change line below if you'd like a range (6 OR 7 character LPs, however false readings increase dramatically)
                    if 7 <= len(lp_str) <= 7:

                        print('\t\tLP: %s' % lp_str)
                        # get the timestamp based off of the frameImg filename
                        file_name = os.path.basename(
                            event.src_path).split('.')[0]
                        video_date = file_name.split('_')[0]
                        print(video_date)
                        camera_location = file_name.split('-')[-2]
                        print(camera_location)
                        video_time = '-'.join(
                            file_name.split('_')[1].split('-')[0:3])
                        print(video_time)
                        datetime_string = '{} {}'.format(
                            video_date, video_time)
                        print(datetime_string)
                        video_start_time = datetime.datetime.strptime(
                            datetime_string, '%Y-%m-%d %H-%M-%S').timestamp()
                        print(video_start_time)
                        frame_time_seconds = (round(
                            int(file_name.split('-')[-1].split('_')[0]) / 4) -
                                              1)
                        if frame_time_seconds < 0:
                            frame_time_seconds = 0
                        print(frame_time_seconds)
                        tz = pytz.timezone('America/Los_Angeles')
                        start_time_seconds = datetime.datetime.fromtimestamp(
                            video_start_time, tz)
                        print(start_time_seconds)
                        print(int(start_time_seconds.strftime("%s")))
                        detection_time = (
                            int(start_time_seconds.strftime("%s")) +
                            frame_time_seconds)
                        print(detection_time)

                        matching_poll = db.polls.find_one(
                            {"unixTS": detection_time})
                        if matching_poll:
                            print(matching_poll)
                            existing_plate = db.plates.find_one(
                                {"plateContent": lp_str})
                            if existing_plate:
                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'driveID':
                                    matching_poll['driveID'] or undefined,
                                    'speed':
                                    matching_poll['speed'],
                                    'power':
                                    matching_poll['power'],
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    existing_plate['_id'],
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/backups/RecentClips/%s.mp4' %
                                    ('-'.join('_'.join(
                                        file_name.split('_')[:-2]).split('-')
                                              [:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One plate detection added: {0}'.format(
                                    result.inserted_id))
                            else:
                                new_plate = {'plateContent': lp_str}
                                plate_id = db.plates.insert_one(
                                    new_plate).inserted_id
                                post_data = {
                                    'plateContent':
                                    lp_str,
                                    'ts':
                                    detection_time,
                                    'cameraLocation':
                                    camera_location,
                                    'pollID':
                                    matching_poll['_id'],
                                    'location':
                                    matching_poll['location'],
                                    'driveID':
                                    matching_poll['driveID'] or undefined,
                                    'speed':
                                    matching_poll['speed'],
                                    'power':
                                    matching_poll['power'],
                                    'heading':
                                    matching_poll['heading'],
                                    'status':
                                    matching_poll['status'],
                                    'detectionImgPath':
                                    event.src_path,
                                    'plateID':
                                    plate_id,
                                    'vehicleImgPath':
                                    '/tesladrive/detections/vehicles/%s.png' %
                                    '_'.join(file_name.split('_')[:-1]),
                                    'secondsIntoVideo':
                                    frame_time_seconds,
                                    'videoFileName':
                                    '/tesladrive/backups/RecentClips/%s.mp4' %
                                    ('-'.join('_'.join(
                                        file_name.split('_')[:-2]).split('-')
                                              [:-1]))
                                }

                                result = plate_detections.insert_one(post_data)
                                print('One detection added: {0}'.format(
                                    result.inserted_id))
                        else:
                            print("no matching poll found")
                    else:
                        print(
                            'plate detected too short, likely false positive: ',
                            lp_str)
                else:
                    print('No characters found')
            except:
                traceback.print_exc()
                sys.exit(1)