Skip to content

Instantly share code, notes, and snippets.

@omeroot
Last active October 28, 2015 07:57
Show Gist options
  • Save omeroot/f89193505a0b35ef55cd to your computer and use it in GitHub Desktop.
Save omeroot/f89193505a0b35ef55cd to your computer and use it in GitHub Desktop.
this service receive photo album it contains images and convert to video
from bottle import route,run,request,static_file
import os
import json
import numpy as np
import cv2
@route('/upload',method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name ,ext = os.path.splitext(upload.filename)
if ext not in ('.png','.jpg','.jpeg'):
return 'File ext'
save_path = os.path.dirname(os.path.realpath(__file__))+'/images/'
save_path+= upload.filename
upload.save(save_path)
algorithm(upload.filename)
print "-----"+upload.filename
return 'OK'
@route('/json')
def send_json():
l = []
save_path = os.path.dirname(os.path.realpath(__file__))+'/images2/'
for file in os.listdir(save_path):
l.append(save_path+str(file))
return json.dumps({'json' : l})
@route('/download/<filename:path>')
def send_files(filename):
path = os.path.dirname(os.path.realpath(__file__))
print "---"+path
print "***"+filename
return static_file(filename,root= path+'/images2/',download=filename)
@route('/')
def hello():
return "HELLO WORLD"
def algorithm(imgname):
save_path = os.path.dirname(os.path.realpath(__file__))+'/images/'
save_path2 = os.path.dirname(os.path.realpath(__file__))+'/images2/'
img = cv2.imread(save_path+imgname, 0)
#transformed_img = cv2.imread(imgname, 0)
for x in range(0, len(img)):
for y in range(0, len(img[0])):
center = img[x,y]
top_left = get_pixel_else_0(img, x-1, y-1)
top_up = get_pixel_else_0(img, x, y-1)
top_right = get_pixel_else_0(img, x+1, y-1)
right = get_pixel_else_0(img, x+1, y )
left = get_pixel_else_0(img, x-1, y )
bottom_left = get_pixel_else_0(img, x-1, y+1)
bottom_right = get_pixel_else_0(img, x+1, y+1)
bottom_down = get_pixel_else_0(img, x, y+1 )
values = thresholded(center, [top_left, top_up, top_right, right, bottom_right,
bottom_down, bottom_left, left])
weights = [1, 2, 4, 8, 16, 32, 64, 128]
res = 0
for a in range(0, len(values)):
res += weights[a] * values[a]
print x
#cv2.imshow('image', img)
cv2.imwrite(os.path.join(save_path2,imgname),img)
#cv2.imshow('thresholded image', transformed_img)
#hist,bins = np.histogram(img.flatten(),256,[0,256])
#cdf = hist.cumsum()
#cdf_normalized = cdf * hist.max()/ cdf.max()
#plt.plot(cdf_normalized, color = 'b')
#plt.hist(transformed_img.flatten(),256,[0,256], color = 'r')
#plt.xlim([0,256])
#plt.legend(('cdf','histogram'), loc = 'upper left')
#plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
#######################################
def thresholded(center, pixels):
out = []
for a in pixels:
if a >= center:
out.append(1)
else:
out.append(0)
return out
def get_pixel_else_0(l, idx, idy, default=0):
try:
return l[idx,idy]
except IndexError:
return default
run(host='0.0.0.0',port=8080,debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment