Пример #1
0
def colormask(image, isday=True, tgtcolr='Default', isdebug=False):
    #
    #image = NormalizeT(image)
    #showResult("normalized",image)
    # blue mask + yellow mask
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    sat = (s.copy()).astype('float')
    val = (v.copy()).astype('float')
    sat /= 255
    val /= 255
    #
    hue = np.zeros(image.shape[:2])
    hue[hue >= 0] = 0.2
    #
    blue1 = h.copy()
    blue2 = h.copy()
    yellow = h.copy()
    #
    if isday:
        blue1 = np.where((h < 105) | (h > 135) | (v < 40) | (s < 40), 0, 255)
        blue2 = np.where((h < 100) | (h > 140) | (v < 30) | (s < 30), 0, 255)
        yellow = np.where((h < 20) | (h > 40) | (v < 40) | (s < 40), 0, 255)
    else:
        blue1 = np.where((h < 105) | (h > 135), 0, 255)
        blue2 = np.where((h < 100) | (h > 140), 0, 255)
        yellow = np.where((h < 20) | (h > 40), 0, 255)
    #
    for case in switch(tgtcolr):
        if case('Blue'):
            hue[blue2 > 0] = 0.5
            hue[blue1 > 0] = 1.0
            hue[yellow > 0] = 0.1
            break
        if case('Yellow'):
            hue[yellow > 0] = 1.0
            hue[blue2 > 0] = 0.1
            hue[blue1 > 0] = 0.15
            break
        if case('Default'):
            hue[blue2 > 0] = 0.4
            hue[blue1 > 0] = 0.9
            hue[yellow > 0] = 0.7
            break
    #
    if isday:
        mask = (hue * sat * val)**2  #(hue*sat*val)**2
    else:
        mask = (hue * sat)**2
    #
    if isdebug:
        showResult("colormask:mask", mask)

    return mask
Пример #2
0
    def detect(self, origin, isdebug=False):
        start = time.time()
        # Default Size
        h, w, c = origin.shape
        size = 200.0
        # Resize
        img = cv2.resize(origin, (int(w * size / h), int(size)))
        #showResult("img",img)
        for case in switch(AnalyzeImageQuality.dayornight(img)):
            if case('Day'):
                # Extract Good Features
                corners = refinedGoodFeatures(origin, img)
                mask = checkFeatures(img, corners, isdebug)
                closing = close(mask)
                refined_gfmask = refine_gfimage(img, closing)
                #showResult("refined_gfmask",refined_gfmask)
                finalmasks = mkfinalmasks(img,
                                          refined_gfmask,
                                          isday=True,
                                          isdebug=isdebug)
                break
            if case('Night'):
                finalmasks = mkfinalmasks(img, None, isday=isdebug)
                break

        for colrindex, fmask in enumerate(finalmasks):
            if (fmask > 0).sum() == 0:
                continue
            bboxes = mask2plates(img, fmask)
            # Resize
            if bboxes is not None:
                bboxes = resizeBBoxes(bboxes, h / size)
                rois = BBoxes2ROIs(origin, bboxes)
                for i, roi in enumerate(rois):
                    confidence = self.licenplatevalidator.process(
                        roi, mode=colrs[colrindex], isdebug=isdebug)
                    print confidence
                    if confidence > 0.7:
                        #pts = self.licenplatevalidator.getRefinedROI()
                        #bbox = refineBBox(bboxes[i],pts)
                        bbox = resizeBBox(bboxes[i], ratio=0.9)
                        print("total elapsed time: " +
                              str(int((time.time() - start) * 1000) / 1000.0) +
                              "s")
                        return confidence, [bbox], [roi]
        '''            
        # Check Result
        if isdebug and bboxes is not None:
            drawBBox(origin,bboxes,debug=True)
            for i in range(len(rois)):
                showResult("cropped",rois[i])
        '''
        return 0.0, None, None
Пример #3
0
def morphological(img, model=cv2.MORPH_OPEN):
    #http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    #cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    #cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
    for case in switch(model):
        if case(cv2.MORPH_OPEN):
            out = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
            break
        if case(cv2.MORPH_CLOSE):
            out = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    return out
Пример #4
0
def FeatureSpace(img, target="LP"):
    # RGB->HSV
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    #blur = cv2.GaussianBlur(s,(5,5),0)
    #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #s_ = np.array(s.shape)
    #s_ = cv2.normalize(s,  s_, 0, 255, cv2.NORM_MINMAX)

    for case in switch(target):
        if case('LP'):
            return s
        if case('VIN'):
            return v
Пример #5
0
def processImg(file_path, filename):
    thisiswhat = app.classifier.run(file_path)
    image = cv2.imread(file_path)
    for case in switch(thisiswhat):
        app.results.append(filename + " :")
        if case('lp'):
            app.results.append("   现场相片")
            # Detect Vehicle
            bbox_car = app.detector.detect(
                opencv2skimage(image))  #mpimg.imread(path)
            if bbox_car is not None:
                img_car = cropImg_by_BBox(image, bbox_car)
                app.results.append(r"车 : 有 ")
                # Detect License Plate
                confidence, bboxes_lp, rois = app.licenseplatedetector.process(
                    img_car)
                markImg = processLP(image, bbox_car, bboxes_lp, confidence)
                cv2.imwrite(file_path, markImg)
            else:
                app.results.append(r"车 : 不全面")
                confidence, bboxes_lp, rois = app.licenseplatedetector.process(
                    image)
                markImg = processLP(image, None, bboxes_lp, confidence)
                if markImg is not None:
                    cv2.imwrite(file_path, markImg)
            break
        if case('vin'):
            app.results.append(r"   车架号")
            app.vehicleidentifier.initialize()
            isFound, confidence, markImg = app.vehicleidentifier.process(image)
            if isFound:
                app.results.append(r"车架号 : 有")
                cv2.imwrite(file_path, markImg)
            else:
                app.results.append(r"车架号 : 没有")
            break
        if case():
            app.results.append(r"   没意思")
            break
Пример #6
0
def upload_file():

    if request.method == 'POST':

        file = request.files['file']

        if file and allowed_file(file.filename):
            # Delete tmp files
            # if exists(app.image_fn):
            #    os.remove(app.image_fn)
            if exists(app.result_fn):
                os.remove(app.result_fn)
            app.results[:] = []
            # save an uploaded file to "uploads" folder
            filename = secure_filename(file.filename)
            app.filename = filename
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], app.filename)
            file.save(file_path)
            #####################
            # Analyze the Image #
            #####################
            start = time.time()
            # Load Image
            image = cv2.imread(file_path)
            # Quality Analyzing
            app.qualityanalyzer.initialize()
            fm, result, explanation = app.qualityanalyzer.process(image)
            for case in switch(explanation):
                if case('Normal'):
                    # Determine what this is
                    processImg(file_path, filename)
                    break
                if case('Blurry'):
                    app.results.append(r"   模糊")
                    break
                if case('Reflective'):
                    app.results.append(r"   不清楚")
                    processImg(file_path, filename)
                    break
                if case('Too Reflective'):
                    app.results.append(r"   曝光过度")
                    break
            end = time.time()
            elapsedtime = end - start
            app.results.append(r"经过时间 : " +
                               str(int(elapsedtime * 1000) / 1000.0) + "s ")
            #######################
            #######################
            # Save Image and Result
            #os.rename(file_path, app.image_fn)
            with open(app.result_fn, 'w') as file:
                for result in app.results:
                    file.write(result)
                    file.write('\n')

            return redirect("/")
            #return redirect(url_for('uploaded_file',
            #                        filename="facedetect-"+filename))

    results = ""
    for result in app.results:
        results += html.result_value_header + result + html.result_value_tail
    image = ""
    if exists(os.path.join(app.config['UPLOAD_FOLDER'], app.filename)):
        image = html.image_header + app.filename + html.image_tail
    return  html.header + \
            html.result_header + \
            results + \
            html.result_tail +\
            html.image_container_header +\
            image + \
            html.image_container_tail + \
            html.tail