~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/samples/python/digits_video.py

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Python 2/3 compatibility
 
4
from __future__ import print_function
 
5
 
 
6
import numpy as np
 
7
import cv2
 
8
 
 
9
# built-in modules
 
10
import os
 
11
import sys
 
12
 
 
13
# local modules
 
14
import video
 
15
from common import mosaic
 
16
 
 
17
from digits import *
 
18
 
 
19
def main():
 
20
    try:
 
21
        src = sys.argv[1]
 
22
    except:
 
23
        src = 0
 
24
    cap = video.create_capture(src)
 
25
 
 
26
    classifier_fn = 'digits_svm.dat'
 
27
    if not os.path.exists(classifier_fn):
 
28
        print('"%s" not found, run digits.py first' % classifier_fn)
 
29
        return
 
30
    model = SVM()
 
31
    model.load(classifier_fn)
 
32
 
 
33
 
 
34
    while True:
 
35
        ret, frame = cap.read()
 
36
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
37
 
 
38
 
 
39
        bin = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 10)
 
40
        bin = cv2.medianBlur(bin, 3)
 
41
        _, contours, heirs = cv2.findContours( bin.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
 
42
        try:
 
43
            heirs = heirs[0]
 
44
        except:
 
45
            heirs = []
 
46
 
 
47
        for cnt, heir in zip(contours, heirs):
 
48
            _, _, _, outer_i = heir
 
49
            if outer_i >= 0:
 
50
                continue
 
51
            x, y, w, h = cv2.boundingRect(cnt)
 
52
            if not (16 <= h <= 64  and w <= 1.2*h):
 
53
                continue
 
54
            pad = max(h-w, 0)
 
55
            x, w = x-pad/2, w+pad
 
56
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0))
 
57
 
 
58
            bin_roi = bin[y:,x:][:h,:w]
 
59
            gray_roi = gray[y:,x:][:h,:w]
 
60
 
 
61
            m = bin_roi != 0
 
62
            if not 0.1 < m.mean() < 0.4:
 
63
                continue
 
64
            '''
 
65
            v_in, v_out = gray_roi[m], gray_roi[~m]
 
66
            if v_out.std() > 10.0:
 
67
                continue
 
68
            s = "%f, %f" % (abs(v_in.mean() - v_out.mean()), v_out.std())
 
69
            cv2.putText(frame, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1)
 
70
            '''
 
71
 
 
72
            s = 1.5*float(h)/SZ
 
73
            m = cv2.moments(bin_roi)
 
74
            c1 = np.float32([m['m10'], m['m01']]) / m['m00']
 
75
            c0 = np.float32([SZ/2, SZ/2])
 
76
            t = c1 - s*c0
 
77
            A = np.zeros((2, 3), np.float32)
 
78
            A[:,:2] = np.eye(2)*s
 
79
            A[:,2] = t
 
80
            bin_norm = cv2.warpAffine(bin_roi, A, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
 
81
            bin_norm = deskew(bin_norm)
 
82
            if x+w+SZ < frame.shape[1] and y+SZ < frame.shape[0]:
 
83
                frame[y:,x+w:][:SZ, :SZ] = bin_norm[...,np.newaxis]
 
84
 
 
85
            sample = preprocess_hog([bin_norm])
 
86
            digit = model.predict(sample)[0]
 
87
            cv2.putText(frame, '%d'%digit, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1)
 
88
 
 
89
 
 
90
        cv2.imshow('frame', frame)
 
91
        cv2.imshow('bin', bin)
 
92
        ch = cv2.waitKey(1) & 0xFF
 
93
        if ch == 27:
 
94
            break
 
95
 
 
96
if __name__ == '__main__':
 
97
    main()