Skip to content

Commit

Permalink
mod
Browse files Browse the repository at this point in the history
  • Loading branch information
hitokun-s committed Apr 3, 2016
1 parent 14a0d90 commit 4e69011
Show file tree
Hide file tree
Showing 28 changed files with 13,662 additions and 130 deletions.
36 changes: 36 additions & 0 deletions imagenet/food_hack/create_train_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import os
import sys

import numpy
from PIL import Image
import six.moves.cPickle as pickle

train_image_dir = "data/bbox_image/"

# ---------------------------------------------------
# create train.txt
import targets
targets = targets.getTargets()
if os.path.exists("train.txt"):
os.remove("train.txt")
f = open("train.txt","w")
for fileName in os.listdir(train_image_dir):
filePath = train_image_dir + fileName
try:
numpy.asarray(Image.open(filePath)).transpose(2, 0, 1)
except:
print "broken file"
os.remove(filePath)
continue
wnid = fileName[:fileName.index("_")]
print "wnid:%s" % wnid
if wnid in targets:
print "find target!:%s" % fileName
classIdx = targets.index(wnid)
f.write((train_image_dir + "%s %d") % (fileName, classIdx) + "\n")
f.close()
# ---------------------------------------------------
3 changes: 2 additions & 1 deletion imagenet/food_hack/resize_and_compute_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
print "file:%s" % filepath
print sys.exc_info()[0]
continue

if image is None:
continue
if sum_image is None:
sum_image = numpy.ndarray(image.shape, dtype=numpy.float32)
sum_image[:] = image
Expand Down
2 changes: 1 addition & 1 deletion imagenet/food_hack/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def getTargets():
# "n07818422", # garlic
# "n07711799", # potato
# "n07721018", # green pepper
"n07758680", # grape
# "n07758680", # grape

]
2 changes: 2 additions & 0 deletions imagenet/food_hack/web/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
48 changes: 27 additions & 21 deletions imagenet/food_hack/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@

from flask import Flask, request, send_from_directory
from flask import jsonify
import ssd
from PIL import Image

# add project base dir to module search path ( for importing char74k, originalOCR)
tgt_dir = os.path.dirname(os.getcwd())
sys.path.append(tgt_dir)
print "path added:%s" % tgt_dir

from char74k.binarize import *
from originalOCR.predict import *
# from char74k.binarize import *
# from originalOCR.predict import *

app = Flask(__name__, static_url_path='/static')

@app.route('/')
def root():
print "get an access!"
return send_from_directory('', "index.html")

@app.route('/<path:path>')
Expand All @@ -29,30 +32,33 @@ def send_js(path):

@app.route('/query', methods=['POST'])
def query():

png_recovered = request.form["img"].decode("base64")

# cStringIOオブジェクト(ファイルのように文字列を読み書きできる)に変換する必要がある
image_string = cStringIO.StringIO(png_recovered)
img = Image.open(image_string)

grayScaled = toGrayScale(numPyToPIL(img))
standardized = standardize(grayScaled)
grayed = pilToNumPy(standardized)
grayed = binarize(grayed) # 戻り値はndarray
if should_invert(grayed):
grayed = invert(grayed)
# print grayed
try:
prediction = predict(np.array([grayed]))
classIdx = prediction[0][0]
confidence = prediction[1][0]
answer = class_labels[classIdx]
print answer
except:
print traceback.format_exc() # stacktrace

return jsonify(dict(answer=answer, confidence=int(confidence * 100)))
res = ssd.predict(img)
return jsonify(res)
#
# grayScaled = toGrayScale(numPyToPIL(img))
# standardized = standardize(grayScaled)
# grayed = pilToNumPy(standardized)
# grayed = binarize(grayed) # 戻り値はndarray
# if should_invert(grayed):
# grayed = invert(grayed)
# try:
# prediction = predict(np.array([grayed]))
# classIdx = prediction[0][0]
# confidence = prediction[1][0]
# answer = class_labels[classIdx]
# print answer
# except:
# print traceback.format_exc() # stacktrace
#
# return jsonify(dict(answer=answer, confidence=int(confidence * 100)))


if __name__ == '__main__':
app.run()
# app.run()
app.run(host='0.0.0.0', port=5000)
132 changes: 25 additions & 107 deletions imagenet/food_hack/web/index.html
Original file line number Diff line number Diff line change
@@ -1,115 +1,33 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<title>OCR</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.min.css">
<title>FoodHack</title>
<meta charset='utf-8'>
<link rel="stylesheet" href="static/main.css" type="text/css" media="all">
<script type="text/javascript" src="static/js/jquery.min.js"></script>
<script type="text/javascript">
var canvas, ctx;

var clearImage = function(){
ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
$("#answer").text("?");
$("#confidence").text("?");
}

window.onload = function () {
canvas = document.getElementById('canvas1');
if (!canvas || !canvas.getContext) {
return false;
}
ctx = canvas.getContext('2d');
clearImage();

var isDrawing = false;

document.ontouchmove = document.onmousemove = function (e) {
e.preventDefault();
}

canvas.ontouchmove = canvas.onmousemove = function (e) {
if (isDrawing) {
ctx.beginPath();
ctx.arc(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop, 10, 0, Math.PI * 2, false);
ctx.stroke();
ctx.fillStyle = '#000';
ctx.fill();
}
e.preventDefault();
}
canvas.ontouchstart = canvas.onmousedown = function (e) {
isDrawing = true;
}
canvas.ontouchend = canvas.onmouseup = function (e) {
isDrawing = false;
}
canvas.onmouseout = function (e) {
isDrawing = false;
}
};

var saveImage = function() {
var imgdata = canvas.toDataURL("image/png"); // デフォルトだとpng, 引数でjpegとかも可能
imgdata = imgdata.replace('data:image/png;base64,', ''); // 頭のいらない部分を落とす
$.ajax({
type: "POST",
url: "query",
data: {"img": imgdata},
success: function (data) {
$("#answer").text(data.answer);
$("#confidence").text(data.confidence);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("got error!");
alert(errorThrown);
}
});
}
</script>
<style>
html,body{
overflow: hidden;
height: 100%
}
body{
padding: 20px;
}
#canvas1{
border: solid 3px #000;
}
.center-block{
width:300px;
margin-top: 20px;
}
#answer-wrapper{
vertical-align: middle;
font-size: 1.5em;
}
#answer{
color: red;
}
#canvas1{
cursor: pointer;
}
</style>
<script src="static/capture.js">
</script>
</head>
<body>
<div class="container">
<h1>OCR</h1>
<p>Trained by Convolutional Neural Network.</p>
<p>Please write a single alphabet or number, and push 'Query'.</p>
<p>You will get an answer with confidence(%).</p>
<div class="center-block">
<canvas id="canvas1" width="300" height="300"></canvas>
<div>
<button class="btn btn-primary" onclick="saveImage();">Query</button>
<button class="btn btn-danger" onclick="clearImage();">Clear</button>
<text id="answer-wrapper">Answer:<span id="answer">?</span>(<span id="confidence">?</span>%)</text>
</div>
</div>
<div class="contentarea">
<div class="camera">
<video id="video">Video stream not available.</video>
<button id="startbutton">Take photo</button>
</div>
<canvas id="canvas">
</canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box.">
</div>
<button id="capture">capture</button>
</div>

<script type="text/javascript">
$(function(){
$("#capture").click(function(){
console.log("take picture");
tp();
});
})
</script>
</body>
</html>
115 changes: 115 additions & 0 deletions imagenet/food_hack/web/index_ocr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html>
<head>
<title>OCR</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.min.css">
<script type="text/javascript" src="static/js/jquery.min.js"></script>
<script type="text/javascript">
var canvas, ctx;

var clearImage = function(){
ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
$("#answer").text("?");
$("#confidence").text("?");
}

window.onload = function () {
canvas = document.getElementById('canvas1');
if (!canvas || !canvas.getContext) {
return false;
}
ctx = canvas.getContext('2d');
clearImage();

var isDrawing = false;

document.ontouchmove = document.onmousemove = function (e) {
e.preventDefault();
}

canvas.ontouchmove = canvas.onmousemove = function (e) {
if (isDrawing) {
ctx.beginPath();
ctx.arc(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop, 10, 0, Math.PI * 2, false);
ctx.stroke();
ctx.fillStyle = '#000';
ctx.fill();
}
e.preventDefault();
}
canvas.ontouchstart = canvas.onmousedown = function (e) {
isDrawing = true;
}
canvas.ontouchend = canvas.onmouseup = function (e) {
isDrawing = false;
}
canvas.onmouseout = function (e) {
isDrawing = false;
}
};

var saveImage = function() {
var imgdata = canvas.toDataURL("image/png"); // デフォルトだとpng, 引数でjpegとかも可能
imgdata = imgdata.replace('data:image/png;base64,', ''); // 頭のいらない部分を落とす
$.ajax({
type: "POST",
url: "query",
data: {"img": imgdata},
success: function (data) {
$("#answer").text(data.answer);
$("#confidence").text(data.confidence);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("got error!");
alert(errorThrown);
}
});
}
</script>
<style>
html,body{
overflow: hidden;
height: 100%
}
body{
padding: 20px;
}
#canvas1{
border: solid 3px #000;
}
.center-block{
width:300px;
margin-top: 20px;
}
#answer-wrapper{
vertical-align: middle;
font-size: 1.5em;
}
#answer{
color: red;
}
#canvas1{
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>OCR</h1>
<p>Trained by Convolutional Neural Network.</p>
<p>Please write a single alphabet or number, and push 'Query'.</p>
<p>You will get an answer with confidence(%).</p>
<div class="center-block">
<canvas id="canvas1" width="300" height="300"></canvas>
<div>
<button class="btn btn-primary" onclick="saveImage();">Query</button>
<button class="btn btn-danger" onclick="clearImage();">Clear</button>
<text id="answer-wrapper">Answer:<span id="answer">?</span>(<span id="confidence">?</span>%)</text>
</div>
</div>
</div>

</body>
</html>
Loading

0 comments on commit 4e69011

Please sign in to comment.