-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pizi
committed
Dec 5, 2019
1 parent
1699f5c
commit e5e0a28
Showing
19 changed files
with
572 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
*.log | ||
*.log.* | ||
config.py | ||
*.pth | ||
.idea/ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import cv2 | ||
import time | ||
import config | ||
from logger import Logger | ||
from push import Push | ||
from storage import Storage | ||
from camera import Camera | ||
|
||
log = Logger.getLogger("Giao") | ||
|
||
|
||
def main(): | ||
# open camera | ||
cap = cv2.VideoCapture(0) | ||
|
||
last_send_time = 0 | ||
|
||
camera = Camera(config.TinyFaceModelPath, config.TinyFaceModelDevice) | ||
push = Push(config.dingTalkWebhookAccessToken) | ||
storage = Storage(config.SinaSCSAccessKey, config.SinaSCSSecretKey, config.SinaSCSBucketName, | ||
config.SinaSCSBucketUrl) | ||
|
||
while (cap.isOpened()): | ||
try: | ||
ret, img = cap.read() | ||
if ret == True: | ||
# img = img[config.MonitorRegion] | ||
imgRegion = img[185:305, 270:490] | ||
diff = camera.detectDiff(imgRegion) | ||
if diff < 150: | ||
log.info("different < 150") | ||
time.sleep(0.2) | ||
continue | ||
bboxes = camera.detectFaces(imgRegion) | ||
log.info("bboxes len %d", len(bboxes)) | ||
if len(bboxes) > 0 and (time.time() - last_send_time) > 30: | ||
last_send_time = time.time() | ||
saveName = str(time.strftime('images/%Y%m%d_%H_%M_%S.png')) | ||
cv2.imwrite(saveName, img) | ||
saveName = str(time.strftime('images/%Y%m%d_%H_%M_%S.jpg')) | ||
cv2.imwrite(saveName, img) | ||
url = storage.saveImage(saveName) | ||
log.info("send giao! %s" % url) | ||
push.sendImage(config.PushTitle, url) | ||
except Exception as e: | ||
log.error("error: %s", e) | ||
|
||
time.sleep(0.5) | ||
|
||
cap.release() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from tinyface import TinyFace | ||
import cv2, time | ||
import numpy as np | ||
from logger import Logger | ||
|
||
log = Logger.getLogger('camera') | ||
|
||
|
||
class Camera(object): | ||
|
||
def __init__(self, modelPath, device='cpu'): | ||
self.TF = TinyFace(modelPath, device=device) | ||
self.lastImg = None | ||
|
||
def detectDiff(self, img): | ||
tStart = time.time() | ||
if self.lastImg is None: | ||
ret, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) | ||
binary_img = binary_img / 255 | ||
self.lastImg = binary_img | ||
return 99999999 | ||
diff = 0 | ||
ret, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) | ||
if self.lastImg is not None: | ||
binary_img = binary_img / 255 | ||
diff = binary_img - self.lastImg | ||
diff = np.abs(np.sum(diff)) | ||
self.lastImg = binary_img | ||
else: | ||
self.lastImg = binary_img / 255 | ||
|
||
log.info("diff: %d, using %.6f sec" % (diff, time.time() - tStart)) | ||
|
||
return diff | ||
|
||
def detectFaces(self, img): | ||
imgRegion = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
bboxes = self.TF.detect_faces(imgRegion, conf_th=0.9, scales=[1]) | ||
return bboxes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# dingTalk webhook access token | ||
dingTalkWebhookAccessToken = 'xxx' | ||
|
||
# the region size need to monitor | ||
# [x, y, w, h] | ||
MonitorRegion = [-1, -1, -1, -1] | ||
|
||
# TinyFace model path | ||
TinyFaceModelPath = './checkpoint_50.pth' | ||
# cpu or cuda | ||
TinyFaceModelDevice = 'cpu' | ||
|
||
# Sina SCS access key, secret key and bucket name | ||
SinaSCSAccessKey = 'xxx' | ||
SinaSCSSecretKey = 'xxx' | ||
SinaSCSBucketName = 'xxx' | ||
SinaSCSBucketUrl = 'http://xxx.xxx/xxx/%s' | ||
|
||
# push title | ||
PushTitle = '[进入预警]' | ||
|
||
# Logger config json text | ||
LoggerJsonConfig = '''{ | ||
"version":1, | ||
"disable_existing_loggers":false, | ||
"formatters":{ | ||
"simple":{ | ||
"format":"[%(asctime)s][%(funcName)-15s:%(lineno)-4s] - %(name)-10s - %(levelname)-5s : %(message)s" | ||
} | ||
}, | ||
"handlers":{ | ||
"console":{ | ||
"class":"logging.StreamHandler", | ||
"level":"DEBUG", | ||
"formatter":"simple", | ||
"stream":"ext://sys.stdout" | ||
}, | ||
"info_file_handler":{ | ||
"class":"logging.handlers.RotatingFileHandler", | ||
"level":"INFO", | ||
"formatter":"simple", | ||
"filename":"info.log", | ||
"maxBytes":10485760, | ||
"backupCount":20, | ||
"encoding":"utf8" | ||
}, | ||
"error_file_handler":{ | ||
"class":"logging.handlers.RotatingFileHandler", | ||
"level":"ERROR", | ||
"formatter":"simple", | ||
"filename":"errors.log", | ||
"maxBytes":10485760, | ||
"backupCount":20, | ||
"encoding":"utf8" | ||
} | ||
}, | ||
"loggers":{ | ||
"my_module":{ | ||
"level":"ERROR", | ||
"handlers":["info_file_handler"], | ||
"propagate":"no" | ||
} | ||
}, | ||
"root":{ | ||
"level":"INFO", | ||
"handlers":["console","info_file_handler","error_file_handler"] | ||
} | ||
}''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.png | ||
*.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import logging, logging.config | ||
import json, config | ||
|
||
class Logger(object): | ||
|
||
@staticmethod | ||
def getLogger(name): | ||
conf = json.loads(config.LoggerJsonConfig) | ||
logging.config.dictConfig(conf) | ||
return logging.getLogger(name) | ||
|
||
if __name__ == '__main__': | ||
l = Logger.getLogger("test") | ||
l.info("aaa") | ||
l = Logger.getLogger("testa") | ||
l.info("aaa") | ||
l = Logger.getLogger("test.a") | ||
l.info("aaa") | ||
l.debug("ddd") | ||
l.warning("w") | ||
l.error('e') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dingtalkchatbot.chatbot import DingtalkChatbot | ||
|
||
|
||
class Push(object): | ||
|
||
def __init__(self, token): | ||
self.d = DingtalkChatbot( | ||
'https://oapi.dingtalk.com/robot/send?access_token=%s' % token) | ||
|
||
def sendImage(self, title, url, is_at_all=False): | ||
self.d.send_markdown(title=title, text='Giao, Giao, Giao!\n![Giao](' + url + ')\n', is_at_all=is_at_all) | ||
|
||
def sendMessage(self, msg, is_at_all=False): | ||
self.d.send_text(msg=msg, is_at_all=is_at_all) | ||
|
||
|
||
if __name__ == '__main__': | ||
push = Push("a") | ||
push.sendImage("a", "b") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
torch>=0.4.1 | ||
torchvision>=0.2.1 | ||
DingtalkChatbot>=1.3.0 | ||
opencv-python>=3.4.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from sinastorage.bucket import SCSBucket | ||
import sinastorage | ||
import time | ||
|
||
|
||
class Storage(object): | ||
|
||
def __init__(self, accessKey, secretKey, bucketName, bucketUrl): | ||
sinastorage.setDefaultAppInfo(accessKey, secretKey) | ||
self.s = SCSBucket(bucketName) | ||
self.bucketUrl = bucketUrl | ||
|
||
def saveImage(self, path): | ||
name = str(time.strftime('%Y%m%d/%H:%M:%S.jpg')) | ||
self.s.putFile(name, path, acl='public-read') | ||
return self.bucketUrl % name | ||
|
||
# def putFileCallback(size, progress): | ||
# print(size, progress) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import cv2 | ||
import time | ||
|
||
cap = cv2.VideoCapture(0) | ||
|
||
cv2.namedWindow("Image") | ||
while(cap.isOpened()): | ||
ret,img = cap.read() | ||
if ret == True: | ||
cv2.imshow('Image',img) | ||
k = cv2.waitKey(100) | ||
if k == ord('a') or k == ord('A'): | ||
|
||
cv2.imwrite(str(time.strftime('%Y%m%d_%H_%M_%S.png')),img) | ||
break | ||
cv2.waitKey(0) | ||
# if cap.isOpened(): | ||
# time.sleep(15) | ||
# ret,img = cap.read() | ||
# if ret == True: | ||
# cv2.imwrite('test2.png',img) | ||
|
||
cap.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from dingtalkchatbot.chatbot import DingtalkChatbot | ||
import sys | ||
sys.path.append("../") | ||
import config | ||
|
||
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=' + config.dingTalkWebhookAccessToken | ||
xiaoding = DingtalkChatbot(webhook) | ||
# Text MSG and at all | ||
# xiaoding.send_text(msg='Giao!看我头像') | ||
|
||
xiaoding.send_markdown(title='x', text='![Giao](http://xxx/20191205/11:08:13.jpg)\n', | ||
is_at_all=True) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from sinastorage.bucket import SCSBucket | ||
import sinastorage | ||
import sys | ||
sys.path.append("../") | ||
import config | ||
import time | ||
|
||
uploadedAmount = 0.0 | ||
def putFileCallback(size, progress): | ||
print(size, progress) | ||
|
||
sinastorage.setDefaultAppInfo(config.SinaSCSAccessKey, config.SinaSCSSecretKey) | ||
|
||
s = SCSBucket(config.SinaSCSBucketName) | ||
# s.putFile('1.jpg', 'selfie.jpg', putFileCallback) | ||
ret = s.putFile(str(time.strftime('%Y%m%d/%H:%M:%S')) + '.jpg', 'selfie.jpg', acl='public-read') | ||
print('complete!') | ||
print(ret.info()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sys | ||
sys.path.append("../") | ||
import time, cv2 | ||
from tinyface import TinyFace | ||
|
||
# load image with cv in RGB. | ||
# IMAGE_PATH = 'selfie.jpg' | ||
IMAGE_PATH = 'selfie.jpg' | ||
img = cv2.imread(IMAGE_PATH) | ||
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
|
||
# load detectors. | ||
DET3 = TinyFace('../checkpoint_50.pth', device='cpu') | ||
|
||
# Tiny Face returns bboxes. | ||
t = time.time() | ||
bboxes = DET3.detect_faces(img, conf_th=0.9, scales=[1]) | ||
print('Tiny Face : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t)) | ||
print("bboxes: ", bboxes) | ||
sizes = [] | ||
for box in bboxes: | ||
sizes.append((box[2] - box[0]) * (box[3] - box[1])) | ||
print(min(sizes)) | ||
print(max(sizes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pth |
Oops, something went wrong.