Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB]오늘의 발전 #40

Open
17011813 opened this issue Jun 7, 2019 · 1 comment
Open

[WEB]오늘의 발전 #40

17011813 opened this issue Jun 7, 2019 · 1 comment

Comments

@17011813
Copy link

17011813 commented Jun 7, 2019

3

Powered By - AICOCO 로바꿔서 클릭하면 출처 우리 깃으로 들어가진다 대박신기하지~!~!~!

주석 달은 server코드
from keras.applications import inception_v3,imagenet_utils
import cv2
import numpy as np
from flask import Flask, request, make_response,jsonify
import numpy as np
import json
import urllib.request
from urllib.request import Request, urlopen
import base64
import numpy as np
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import logging

model = None
app = Flask(name,static_url_path='')

def preprocess_img(img,target_size=(299,299)): #img가공
if (img.shape[2] == 4):
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
img = cv2.resize(img,target_size) #img사이즈 바꿔줌
img = np.divide(img,255.)
img = np.subtract(img,0.5)
img = np.multiply(img,2.)
return img

def load_im_from_url(url): #img를 url주소에 들어가서 사진만 가져오기
requested_url = urlopen(Request(url,headers={'User-Agent': 'Mozilla/5.0'}))
image_array = np.asarray(bytearray(requested_url.read()), dtype=np.uint8)
print (image_array.shape)
print (image_array)
image_array = cv2imdecode(image_array, -1)
print (image_array.shape)
return image_array

def load_im_from_system(url): #img를 array로 길게 문자열로 바꿔주는 거같음
image_url = url.split(',')[1]
image_url = image_url.replace(" ", "+")
image_array = base64.b64decode(image_url)
#image_array = np.asarray(bytearray(image_array), dtype=np.uint8)
image_array = np.fromstring(image_array, np.uint8)
image_array = cv2.imdecode(image_array, -1) #img를 decode해주고 밑에서 최종 return
return image_array

def predict(img): #우리 코드 학습
img=preprocess_img(img) #가공된 이미지 넣어주고
print (img.shape)
global model
if model is None: #여기가 학습 체크포인트로 import하든, 여기다 함수써서 코드 가져오든 둘중~!
model =inception_v3.InceptionV3()
model.compile(optimizer='adam', loss='categorical_crossentropy')
preds = model.predict(np.array([img])) #img배열을 불러와서 이 predict함수에 넣은 다음에
return imagenet_utils.decode_predictions(preds)
#예측한 pred 값 decode해서 리턴 : (이름,확률) 이렇게 저장되있는거 같음

@app.route('/classify_system', methods=['GET'])
def classify_system():
image_url = request.args.get('imageurl')
image_array = load_im_from_system(image_url) #위에 load함수 써서 image를 array로 저장
resp = predict(image_array) #위의 predict함수에 img넣고 예측한 값을 resp에 저장
result = []
print(resp[0]) #어떻게 저장되어있는지 확인하면 그 형식대로 predict돌리고 나서 저장할때 resp꼴 맞게 코드 바꿔야됨
for r in resp[0]: #resp에 이름이랑 확률 들어있음 ___ r로 스캔하는데 r[1]에는 이름
result.append({"class_name":r[1],"score":float(r[2])}) #r[2]에는 확률 저장되게 포문돌아감~
return jsonify({'results':result})

@app.route('/classify_url', methods=['GET']) #url과 system이 뭐가 다를까?
def classify_url():
image_url = request.args.get('imageurl')
image_array = load_im_from_url(image_url)
resp = predict(image_array)
result = []
for r in resp[0]:
result.append({"class_name":r[1],"score":float(r[2])})
return jsonify({'results':result})

@app.route('/classify-system', methods=['GET'])
def do_system():
return app.send_static_file('system.html')

@app.route('/classify-url', methods=['GET'])
def do_url():
return app.send_static_file('url.html')

@app.route('/', methods=['GET'])
def root():
return app.send_static_file('index.html')

if name == 'main':
app.run(host='127.0.0.1', port=60, debug=True)

url과 system 코드 img load 데이터만 코드 다른데 차이가 뭔지 잘 모르겠음....
http://127.0.0.1:60/classify-url
우리 웹사이트~~
check point 활용 방법 알고 저기 predict만 고치면 될거같은데 우선 원래 웹도 계속 판독만 해서 빨리 해봐야 될거같다

@17011813
Copy link
Author

https://blog.naver.com/a952kr/221553420397
체크 포인트

@PMH2906 PMH2906 changed the title 오늘의 발전 [WEB]오늘의 발전 Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant