forked from NCNU-OpenSource/fang-dau-shi-tung
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.py
77 lines (63 loc) · 2.26 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from time import time
import cv2
import numpy as np
import ConfigParser
import time
import RPi.GPIO as GPIO
import time
import picamera
ROOT_NODE = -1
def buzzier():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 50)
p.start(50)
p.ChangeFrequency(659)
time.sleep(1)
p.stop()
GPIO.cleanup()
def get_frame():
img = open("image/1.jpg", "rb").read()
return img
def countObj():
if os.stat('image/0.jpg').st_size > 0:
fidelity = False
fidelityValue = .7
img = cv2.imread('image/0.jpg')
imgCopy = img.copy()
img = cv2.medianBlur(img, 15)
#img = cv2.adaptiveBilateralFilter(img, (5, 5), 150) # Preserve edges
#img = cv2.blur(img, (3,3))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#imgt = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 10)
_, imgt = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
#_, imgt = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY_INV)
imgt = cv2.morphologyEx(imgt, cv2.MORPH_OPEN, (5, 5))
img2 = imgt.copy()
c, h = cv2.findContours(img2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
fidelityRange = 0
if fidelity:
maxArea = .0;
for i in c: # With images it is convenient to know the greater area
area = cv2.contourArea(i)
if area > maxArea:
maxArea = area
fidelityRange = maxArea - (maxArea * fidelityValue); # If objects have same size it prevents false detection
totalContours = 0
br = []
for i in xrange(len(c)):
if h[0][i][3] == ROOT_NODE and cv2.contourArea(c[i]) >= fidelityRange:
totalContours += 1
approx = cv2.approxPolyDP(c[i], 3, True)
br.append(cv2.boundingRect(approx))
for b in br:
cv2.rectangle(imgCopy, (b[0], b[1]), (b[0] + b[2], b[1] + b[3]), (255, 255, 0), 3)
cv2.imwrite('image/1.jpg', imgCopy)
print 'Total contours: ', totalContours
return totalContours
else:
return -1