-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
60 lines (47 loc) · 1.53 KB
/
run.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
# -*- coding: utf-8 -*-
'''
Author: Pongsakorn Sommalai (bongtrop@gmail.com)
Date: 25/11/2015
Description: Server blur beer photo
'''
# Default
import json
import cgi
import random
import time
import detect
# Web Server
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
def gen_random_name(l):
return ''.join([random.choice('0123456789ABCDEF') for x in range(l)])
class Blur(Resource):
isLeaf = True
def render_GET(self, request):
try:
f = open("out.jpg", "rb")
request.setHeader("content-type", "image/jpeg")
return f.read()
except Exception as error:
return "No Init"
def render_POST(self, request):
#try:
self.headers = request.getAllHeaders()
img = cgi.FieldStorage(
fp = request.content,
headers = self.headers,
environ = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE': self.headers['content-type']})
filename = gen_random_name(32)+'.jpg'
out = open('images/'+filename, 'wb')
out.write(img["image"].value)
out.close()
rects = detect.beer('images/'+filename)
return json.dumps({"status": "success", "data": rects})
#except Exception as error:
# request.setResponseCode(400)
# return json.dumps({"status": "fail", "data": str(error)})
resource = Blur()
factory = Site(resource)
reactor.listenTCP(10002, factory)
reactor.run()