2

Me and my project members are trying do develop a small application for traffic sign recognition with OpenCV and python. Our goal is that each traffic sign can be detected by realtime (webcam) or video file with a low percentage of errors for our student project. We decided to work with the haarlike feature to train different signs such as "stop-sign", "priority-sign" and so on. The result is a XML-File for each specific sign which can be used to detect.

The problem is now to combine these XML-files in one python script. We also saw the "facedetection.py" which shows only two haar classifiers which are interleaved and only spotted in one big green rectangle and inside this is another blue one for eye detection.

The question is if it´s possible to combine the 30 traffic sign XML-files in a python script, by using threads or other solutions?

I´m a newbie in python so I tried to combine three classifier which are independently to each other, but the result is really bad because the more xml-files you implement the less is the speed/benchmark of the program.

#!/usr/bin/env python

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2

# local modules
from video import create_capture
from common import clock, draw_str


def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
                                     flags=cv2.CASCADE_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)

if __name__ == '__main__':
    import sys, getopt
    print(__doc__)
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade=', 'my-cascade='])
try:
    video_src = video_src[0]
except:
    video_src = 0
args = dict(args)
cascade_fn = args.get('--cascade', "/Users/daniel/opencv/data/haarcascades/haarcascade_frontalface_default.xml")
nested_fn  = args.get('--nested-cascade', "/Users/daniel/opencv/data/haarcascades/haarcascade_eye.xml")
my_cascade  = args.get('--my-cascade', "/Users/daniel/Downloads/cascade_speed20_server.xml")

cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)
mycascade = cv2.CascadeClassifier(my_cascade)

cam = create_capture(video_src, fallback='synth:bg=../data/lena.jpg:noise=0.05')

while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    t = clock()
    rects1 = detect(gray, cascade)
    vis1 = img.copy()
    draw_rects(vis1, rects1, (255, 255, 0))
    rects2 = detect(gray, nested)
    vis2 = img.copy()
    draw_rects(vis2, rects2, (0, 255, 0))
    rects3 = detect(gray, mycascade)
    vis3 = img.copy()
    draw_rects(vis3, rects3, (0, 255, 255))
    dt = clock() - t


    draw_str(vis1, (20, 20), 'time: %.1f ms' % (dt*1000))
    draw_str(vis2, (20, 20), 'time: %.1f ms' % (dt*1000))
    draw_str(vis3, (20, 20), 'time: %.1f ms' % (dt*1000))
    cv2.imshow('facedetect', vis1)
    cv2.imshow('facedetect', vis2)
    cv2.imshow('facedetect', vis3)

    if 0xFF & cv2.waitKey(5) == 27:
        break
cv2.destroyAllWindows()
Daniel
  • 552
  • 2
  • 9
  • 29
  • You're going to have very hard time trying to run 30 cascades on full frames of real time video, and according to docs "The function is parallelized with the TBB library." so you're unlikely to gain much by running multiple instances in parallel on top of that. Divide and conquer -- you need to have several phases, reducing the complexity of the problem (or at least the size of the input) in each step. So, for example initially detect any traffic signs (maybe look for circles and triangles). Perhaps even in a scaled down image -- you can afford false positives here. – Dan Mašek May 14 '16 at 23:18
  • Next, work with only the regions of the image where the potential traffic signs were detected, and continue dividing into finer and finer categories. For example first by general shape. Then by colours. At that point, the number of different possibilities to test is going to be a lot smaller, and also you with have a small image to analyze. – Dan Mašek May 14 '16 at 23:21
  • Ah okay, so I think we skipped some important parts as you said. So it´s more like detecting part is identifies the regions of interest and therefore it´s using color segmentation or shape recognition. And after that you can recognize these signs by haar classifier, hog or other classification method. – Daniel May 15 '16 at 10:10

0 Answers0