This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
566 lines (518 loc) · 26.5 KB
/
serve.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
__version__ = "0.1"
__all__ = ["SimpleHTTPRequestHandler"]
import os
import posixpath
import http.server
import urllib.request, urllib.parse, urllib.error
import cgi
import shutil
import mimetypes
import re
import math
from io import BytesIO
import dlib
from skimage.feature import hog
from skimage import data, exposure
from matplotlib import pyplot
from requests_toolbelt.multipart import decoder
import time
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
face_descriptors = {}
step_times = []
labeled_chips = {}
hog_face_detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_5_face_landmarks.dat")
facerec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
trained_detector = None
def do_GET(self):
f = self.send_head()
if f:
self.copyfile(f, self.wfile)
f.close()
def do_HEAD(self):
f = self.send_head()
if f:
f.close()
def do_POST(self):
r, info = self.deal_post_data()
if r:
return
print((r, info, "by: ", self.client_address))
f = BytesIO()
self.write_html_start(f)
f.write(b"<title>Upload Failed</title>\n</head>\n")
f.write(b"<body>\n<h2>Upload Failed</h2>\n")
f.write(b"<hr>\n")
f.write(b"<strong>Failed:</strong>")
f.write(info.encode())
f.write(("<br><a href=\"%s\">back</a>" % self.headers['referer']).encode())
f.write(b"</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
if f:
self.copyfile(f, self.wfile)
f.close()
def deal_post_data(self):
content_type = self.headers['content-type']
data_length = int(self.headers['content-length'])
if not content_type:
return (False, "No Content-Type header")
multipart_data = decoder.MultipartDecoder(self.rfile.read(data_length), content_type)
action = None
filename = None
label = None
mark = {}
for part in multipart_data.parts:
content_disposition = part.headers[b"Content-Disposition"]
if (b"name=\"min" in content_disposition or b"name=\"max" in content_disposition):
mark[re.findall("name=\"(\w*?)\"", content_disposition.decode())[0]] = part.text
if (b"name=\"filename\"" in content_disposition):
filename = part.text
if b"name=\"file\"" in content_disposition:
filename = re.findall(r'.*filename="(.*)"', content_disposition.decode())[0]
print("filename is " + filename)
file_content = part.content
if b"name=\"action\"" in content_disposition:
action = part.text
print("action is " + action)
if b"name=\"label\"" in content_disposition:
label = part.text
print("label is " + label)
if len(mark) > 0 and "minX" in mark and mark["minX"]:
trainpath = os.path.join(self.translate_path(self.path), "train.txt")
try:
out = open(trainpath, 'a')
out.write(str(mark["minX"]) + ";" +str(mark["minY"]) + ";" + str(mark["maxX"]) + ";" + str(mark["maxY"]) + ";" + filename + "\n")
out.close()
except IOError:
return (False, "Can't create file %s to write" % trainpath)
self.display_success(None, "appended Marker for %s to training data" % filename)
return (True, "success")
if not action:
return (False, "Can't find out action")
if (action == "resetTraining"):
trainpath = os.path.join(self.translate_path(self.path), "train.txt")
os.remove(trainpath)
self.display_success(None, "resetted training data")
return (True, "success")
if (action == "doTraining"):
filenames = self.do_training()
self.display_training_files(filenames)
return (True, "success")
if not filename:
return (False, "Can't find out file name")
if (filename.endswith(".py") or filename.endswith(".dat") or filename.endswith(".txt")):
directory = self.translate_path(self.path)
else:
if not (filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png")):
return (False, "Unknown File type (not png, jpeg or jpg)")
filename = filename.lower()
path = self.translate_path(self.path)
directory = os.path.join(path, os.path.splitext(filename)[0])
if not os.path.exists(directory):
os.makedirs(directory)
absolutefile = os.path.join(directory, filename)
try:
out = open(absolutefile, 'wb')
except IOError:
return (False, "Can't create file %s to write" % absolutefile)
out.write(file_content)
out.close()
self.process_uploaded_file(absolutefile, filename, action, label)
return (True, "File '%s' upload success!" % filename)
def process_uploaded_file(self, absoluteFile: str, filename:str, action: str, label: str):
self.step_times.clear()
self.labeled_chips.clear()
if (action == "upload"):
self.display_success(filename, "Successfully uploaded file ")
if (action == "hog"):
self.create_hog_image(absoluteFile, filename)
self.display_files(filename, "hog")
if (action == "facedetection"):
self.detect_faces(absoluteFile, filename)
self.display_files(filename, "facedetection")
if (action == "facelandmark"):
self.landmark_faces(absoluteFile, filename)
self.display_files(filename, "facelandmark")
if (action == "facelabel"):
self.label_face(absoluteFile, filename, label)
self.display_files(filename, label)
if (action == "facerecognition"):
self.recognize_face(absoluteFile, filename)
self.display_files(filename, "facerecognition")
if (action == "markregion"):
self.display_files(filename, None)
if (action == "useTrainedDetector"):
self.use_trained_detector(absoluteFile, filename)
self.display_files(filename, "trainedhog")
def create_hog_image(self, absoluteFile: str, filename: str):
image = dlib.load_grayscale_image(absoluteFile)
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualize=True, multichannel=False)
start_time = time.clock()
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
self.step_times.append("HOG Creation: %.3f s" % (time.clock() - start_time))
processed_file = self.path_to_processed_file(filename, "hog")
pyplot.imsave(processed_file, hog_image_rescaled)
def detect_faces(self, absoluteFile: str, filename: str):
image = dlib.load_rgb_image(absoluteFile)
start_time = time.clock()
face_boundaries = self.hog_face_detector(image, 1)
self.step_times.append("HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
for i, d in enumerate(face_boundaries):
draw_rectangle(image, d)
processed_file = self.path_to_processed_file(filename, "facedetection")
dlib.save_image(image, processed_file)
def landmark_faces(self, absoluteFile: str, filename: str):
image = dlib.load_rgb_image(absoluteFile)
start_time = time.clock()
dets_hog = self.hog_face_detector(image, 1)
self.step_times.append("HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
for i, d in enumerate(dets_hog):
start_time = time.clock()
shape = self.predictor(image, d)
self.step_times.append("Landmark Detection: %.3f s" % (time.clock() - start_time))
draw_marker(image, shape.parts())
processed_file = self.path_to_processed_file(filename, "facelandmark")
dlib.save_image(image, processed_file)
def recognize_face(self, absoluteFile: str, filename: str):
if len(self.face_descriptors) == 0:
return False
image = dlib.load_rgb_image(absoluteFile)
start_time = time.clock()
dets_hog = self.hog_face_detector(image, 1)
self.step_times.append("HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
for i, d in enumerate(dets_hog):
start_time = time.clock()
shape = self.predictor(image, d)
self.step_times.append("Landmark Detection: %.3f s" % (time.clock() - start_time))
face_chip = dlib.get_face_chip(image, shape)
chip_file = self.path_to_processed_file(filename, str(i))
dlib.save_image(face_chip, chip_file)
start_time = time.clock()
face_descriptor = self.facerec.compute_face_descriptor(image, shape)
self.step_times.append("Face Descriptor Computation: %.3f s" % (time.clock() - start_time))
draw_rectangle(image, d)
face_label = "???[" + str(i) + "]"
for label, known_descriptor in self.face_descriptors.items():
distance = 0.0
for i in range(len(face_descriptor)):
distance = distance + (face_descriptor[i] - known_descriptor[i]) * (face_descriptor[i] - known_descriptor[i])
distance = math.sqrt(distance)
if (distance < 0.6):
face_label = label + (" (distance=%.3f)" % distance)
print("Distance to {} is {}".format(label, distance))
self.labeled_chips[face_label] = chip_file
processed_file = self.path_to_processed_file(filename, "facerecognition")
dlib.save_image(image, processed_file)
def label_face(self, absoluteFile: str, filename: str, label: str):
if not label:
return False
image = dlib.load_rgb_image(absoluteFile)
start_time = time.clock()
dets_hog = self.hog_face_detector(image, 1)
self.step_times.append("HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
if len(dets_hog) != 1:
return False
for i, d in enumerate(dets_hog):
start_time = time.clock()
shape = self.predictor(image, d)
self.step_times.append("Landmark Detection: %.3f s" % (time.clock() - start_time))
start_time = time.clock()
face_descriptor = self.facerec.compute_face_descriptor(image, shape)
self.step_times.append("Face Descriptor Computation: %.3f s" % (time.clock() - start_time))
self.face_descriptors[label] = face_descriptor;
draw_rectangle(image, d)
print("labeled face as " + label)
processed_file = self.path_to_processed_file(filename, label)
dlib.save_image(image, processed_file)
def do_training(self):
trainpath = os.path.join(self.translate_path(self.path), "train.txt")
trainfile = open(trainpath, 'r')
testdatadef = trainfile.readlines()
trainfile.close()
result = []
images = []
boxes = []
for testdata in testdatadef:
testvalues = testdata.split(";")
boxes.append([dlib.rectangle(int(testvalues[0]), int(testvalues[1]), int(testvalues[2]), int(testvalues[3]))])
imagepath = os.path.join(self.translate_path(self.path), testvalues[4].strip("\n"))
result.append(testvalues[4])
images.append(dlib.load_grayscale_image(imagepath))
options = dlib.simple_object_detector_training_options()
options.add_left_right_image_flips = True
options.C = 5
# options.be_verbose = False
start_time = time.clock()
SimpleHTTPRequestHandler.trained_detector = dlib.train_simple_object_detector(images, boxes, options)
self.step_times.append("Trained HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
print(type(SimpleHTTPRequestHandler.trained_detector))
return result
def use_trained_detector(self, absoluteFile: str, filename: str):
print(type(SimpleHTTPRequestHandler.trained_detector))
image = dlib.load_rgb_image(absoluteFile)
start_time = time.clock()
dets_hog = SimpleHTTPRequestHandler.trained_detector(image, 1)
self.step_times.append("Using self-trained HOG + SVM Detection: %.3f s" % (time.clock() - start_time))
for i, d in enumerate(dets_hog):
print("detected object: " + str(d))
draw_rectangle(image, d)
processed_file = self.path_to_processed_file(filename, "trainedhog")
dlib.save_image(image, processed_file)
def display_files(self, filename, classifier):
uploaded_file = self.path_to_uploaded_file(filename)
if classifier != None:
processed_file = self.path_to_processed_file(filename, classifier)
f = BytesIO()
self.write_html_start(f)
f.write(b"<title>Processed File result</title>\n</head>\n<body>\n")
if classifier != None:
f.write(("<img src=\"%s\"/>\n" % uploaded_file).encode())
f.write(("<img src=\"%s\"/>\n" % processed_file).encode())
else:
f.write(b"<form ENCTYPE=\"multipart/form-data\" method=\"post\"\n>")
f.write(b"<div id=\"selector\" hidden=\"hidden\" style=\"border: 3px dotted red; position: absolute;\" ")
f.write(b"<div id=\"selector\" hidden=\"hidden\" style=\"border: 3px dotted red; position: absolute;\" ")
f.write(b"onmousedown=\"startMarker(event)\" onmousemove=\"updateMarker(event)\" onmouseup=\"finalizeMarker(event)\"></div>")
f.write(b"<input id=\"minX\" name=\"minX\" type=\"hidden\" /><input id=\"maxX\" name=\"maxX\" type=\"hidden\" />")
f.write(b"<input id=\"minY\" name=\"minY\" type=\"hidden\" /><input id=\"maxY\" name=\"maxY\" type=\"hidden\" />")
f.write(("<input name=\"filename\" type=\"hidden\" value=\"%s\"/>"% uploaded_file).encode())
f.write(b"<div id=\"selector\" hidden=\"hidden\" style=\"border: 1px dotted #000; position: absolute;\"></div>")
f.write(b"<script>var x1, x2, y1, y2; var doSelect=false;\n")
f.write(b"function updateSelector() { minX.value = Math.min(x1,x2); maxX.value = Math.max(x1,x2); minY.value = Math.min(y1,y2); maxY.value = Math.max(y1,y2); ")
f.write(b"selector.style.left = minX.value + 'px'; selector.style.top = minY.value + 'px';")
f.write(b"selector.style.width = (maxX.value - minX.value) + 'px'; selector.style.height = (maxY.value - minY.value) + 'px'; };\n")
f.write(b"function startMarker(e) { doSelect=true; selector.hidden = 0; x1 = e.clientX; y1 = e.clientY; updateSelector();debugEl.textContent=\'started\';};\n")
f.write(b"function updateMarker(e) {if (doSelect) {x2 = e.clientX; y2 = e.clientY; updateSelector(); debugEl.textContent= '(' + x1 +\',\' + y1 + '):(' + x2 +\',\' + y2 + \')\'}};\n")
f.write(b"function finalizeMarker(e) {updateMarker(e); doSelect=false; debugEl.textContent=\'stopped\';};</script>\n")
f.write(("<img src=\"%s\" draggable=\"false\" " % uploaded_file).encode())
f.write(b"onmousedown=\"startMarker(event)\" onmousemove=\"updateMarker(event)\" onmouseup=\"finalizeMarker(event)\"/><span id=\"debugEl\" ></span><br/>")
f.write(b"<input type=\"submit\" name=\"mark\" value=\"Mark region\"/></form>\n")
f.write(b"</form>\n")
if (len(self.labeled_chips) > 0):
f.write(b"<h2>recognition result</h2>\n<table style=\"width:100%\"><tr>\n")
for label, chip_file in self.labeled_chips.items():
f.write(("<td><img src=\"%s\"/></td>\n" % chip_file).encode())
f.write(b"</tr><tr>")
for label, chip_file in self.labeled_chips.items():
f.write(("<td>%s</td>\n" % label).encode())
f.write(b"</tr></table><br><br>\n")
f.write(b"<h2>process times</h2>")
for step_time in self.step_times:
f.write((step_time + "<br>").encode())
f.write(("<br><a href=\"%s\">back</a>" % self.headers['referer']).encode())
f.write(b"</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
self.copyfile(f, self.wfile)
f.close()
def display_training_files(self, filepaths):
f = BytesIO()
self.write_html_start(f)
f.write(b"<title>Processed File result</title>\n</head>\n<body>\n")
f.write(b"<h2>Used files for Training</h2>\n")
for path in filepaths:
f.write(("<img src=\"%s\"/>\n" % path).encode())
f.write(b"<h2>Training Successful</h2>")
for step_time in self.step_times:
f.write((step_time + "<br>").encode())
f.write(("<br><a href=\"%s\">back</a>" % self.headers['referer']).encode())
f.write(b"</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
self.copyfile(f, self.wfile)
f.close()
def display_success(self, filename: str, message: str):
f = BytesIO()
self.write_html_start(f)
f.write(b"<title>Upload Result Page</title>\n</head>\n<body>\n")
f.write(("<strong>%s</strong>" % message).encode())
if filename:
f.write(filename.encode())
f.write(("<br><a href=\"%s\">back</a>" % self.headers['referer']).encode())
f.write(b"</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
if f:
self.copyfile(f, self.wfile)
f.close()
def path_to_processed_file(self, filename, classifier):
return os.path.splitext(filename)[0] + '/' + os.path.splitext(filename)[0] + '_' + classifier + os.path.splitext(filename)[1]
def path_to_uploaded_file(self, filename):
return os.path.splitext(filename)[0] + '/' + filename
def write_html_start(self, f):
f.write(b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write(b"<html>\n<head><link rel=\"stylesheet\" type=\"text/css\" href=\"/bootstrap.min.css\">\n")
def send_head(self):
path = self.translate_path(self.path)
f = None
if os.path.isdir(path):
if not self.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
ctype = self.guess_type(path)
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return None
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return f
def list_directory(self, path):
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
f = BytesIO()
displaypath = cgi.escape(urllib.parse.unquote(self.path))
self.write_html_start(f)
f.write(("<title>Directory listing for %s</title>\n" % displaypath).encode())
f.write(b"<script>function actionChanged() {\n")
f.write(b"if(action.value =='facelabel') {label.hidden=0;} else {label.hidden=1;};")
f.write(b"if(action.value =='doTraining') {upload.hidden=1;} else {upload.hidden=0;};}")
f.write(b"</script>\n")
f.write(b"</head>\n<body onload=\"actionChanged();\">\n<div class=\"container\">\n")
f.write(b"<form ENCTYPE=\"multipart/form-data\" method=\"post\">")
f.write(b"<div class=\"form-group\">\n")
f.write(b"<select id=\"action\" name=\"action\" onchange=\"actionChanged()\"/>")
f.write(b"<option value=\"upload\">Upload</option>")
f.write(b"<option value=\"hog\">Generate HOG</option>")
f.write(b"<option value=\"facedetection\">Detect faces</option>")
f.write(b"<option value=\"facelandmark\">Landmark faces</option>")
f.write(b"<option value=\"facelabel\">Label a face to be used in Recognition</option>")
f.write(b"<option value=\"facerecognition\">Recognize Faces</option>")
f.write(b"<option value=\"markregion\">Create Training Data: Upload and Mark region</option>")
f.write(b"<option value=\"resetTraining\">Reset created training data</option>")
f.write(b"<option value=\"doTraining\">Train HOG detector with created training data</option>")
f.write(b"<option value=\"useTrainedDetector\">Use trained HOG Detector for detection</option>")
f.write(b"</select/>")
f.write(b"</div>\n<div class=\"form-group\">\n")
f.write(b"<input id=\"upload\" name=\"file\" accept=\"image/*;capture=camera\" type=\"file\"/>")
f.write(b"</div>\n<div class=\"form-group\" id=\"label\">\n")
f.write(b"<label for=\"label\">Label</label><input name=\"label\" id=\"label\"/></div>")
f.write(b"<button type=\"submit\" class=\"btn btn-primary\">Ok</button>\n</form>\n</div>\n")
f.write(b"<hr>\n")
f.write(("<h2>Directory listing for %s</h2>\n" % displaypath).encode())
f.write(b"<hr>\n<ul>\n")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write(('<li><a href="%s">%s</a>\n'
% (urllib.parse.quote(linkname), cgi.escape(displayname))).encode())
f.write(b"</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
return f
def translate_path(self, path):
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib.parse.unquote(path))
words = path.split('/')
words = [_f for _f in words if _f]
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
return path
def copyfile(self, source, outputfile):
shutil.copyfileobj(source, outputfile)
def guess_type(self, path):
base, ext = posixpath.splitext(path)
if ext in self.extensions_map:
return self.extensions_map[ext]
ext = ext.lower()
if ext in self.extensions_map:
return self.extensions_map[ext]
else:
return self.extensions_map['']
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
'': 'application/octet-stream', # Default
'.py': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain',
})
def test(HandlerClass = SimpleHTTPRequestHandler,
ServerClass = http.server.HTTPServer):
http.server.test(HandlerClass, ServerClass)
def draw_rectangle(img, rect):
markerpixel = [255,0,0]
for x in range(rect.left(), rect.right()):
point(x, rect.top(), img, markerpixel)
point(x, rect.top() + 1, img, markerpixel)
point(x, rect.top() - 1, img, markerpixel)
point(x, rect.bottom(), img, markerpixel)
point(x, rect.bottom() + 1, img, markerpixel)
point(x, rect.bottom() - 1, img, markerpixel)
for y in range(rect.top(), rect.bottom()):
point(rect.left(), y, img, markerpixel)
point(rect.left() - 1, y, img, markerpixel)
point(rect.left() + 1, y, img, markerpixel)
point(rect.right(), y, img, markerpixel)
point(rect.right() - 1, y, img, markerpixel)
point(rect.right() + 1, y, img, markerpixel)
def point(x, y, img, pixel):
if (x >= 0 and y >= 0):
img[y][x] = pixel
def draw_marker(img, markers):
markerpixel = [0,255,0]
for landmark in enumerate(markers):
for i in range(-6, 7): # line lenght
for j in range(-1, 2): # line width
point(landmark[1].x + i, landmark[1].y + j, img, markerpixel)
point(landmark[1].x + j, landmark[1].y + i, img, markerpixel)
if __name__ == '__main__':
test()