aiServerAPI服务搭建
注意: 5 为正式发布版本,前边为项目演进过程
20181019_v2
lida2012@foxmail.com
1.项目背景
Ai项目网站在整合模型的时候,变得繁重和缓慢,在生成环境和开发环境,都存在瓶颈问题,故将模型相关的服务器抽取成一个API服务,使用Django进行搭建。
2.项目环境
搭建服务器:192.168..
项目路径:/opt/web/aiServer/
Python版本:python3.5
Django版本:django 2.0
3.项目搭建
cd /opt/web
python3 -m pip install virtualenv
virtualenv env_django2.0
source env_django2.0/bin/activate
python -m pip install django==2.0
python3 -m pip install djangorestframework
python -m pip install django-filter
python -m pip install markdown
django-admin.py startproject aiServer
django-admin.py startapp yyai
python3 manage.py migrate
python3 manage.py createsuperuser --email admin@example.com --username admin
: 密码: admin123
(env_django2.0) 192-168-**-**.idc.com [/opt/web/aiServer] 2018-10-11 14:45:10
root@pts/6 # find .
.
./manage.py
./aiServer
./aiServer/wsgi.py
./aiServer/__init__.py
./aiServer/__init__.pyc
./aiServer/settings.pyc
./aiServer/__pycache__
./aiServer/__pycache__/__init__.cpython-35.pyc
./aiServer/__pycache__/settings.cpython-35.pyc
./aiServer/__pycache__/wsgi.cpython-35.pyc
./aiServer/__pycache__/urls.cpython-35.pyc
./aiServer/settings.py
./aiServer/urls.py
./yyai
./yyai/admin.py
./yyai/__init__.py
./yyai/tests.py
./yyai/models.py
./yyai/migrations
./yyai/migrations/__init__.py
./yyai/migrations/__pycache__
./yyai/migrations/__pycache__/__init__.cpython-35.pyc
./yyai/serializers.py [下一节添加]
./yyai/__pycache__
./yyai/__pycache__/__init__.cpython-35.pyc
./yyai/__pycache__/models.cpython-35.pyc
./yyai/__pycache__/admin.cpython-35.pyc
./yyai/__pycache__/urls.cpython-35.pyc
./yyai/__pycache__/views.cpython-35.pyc
./yyai/__pycache__/serializers.cpython-35.pyc
./yyai/\
./yyai/urls.py
./yyai/views.py
./db.sqlite3
4.项目初级开发
4.1.基础设置及开发
cd /opt/web/aiServer/aiServer
vim settings.py
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'yyai',
)
vim urls.py
from django.conf.urls import include, url
from django.contrib import admin
from rest_framework import routers
from yyai import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = [
# Examples:
# url(r'^$', 'aiServer.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
#url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
cd /opt/web/aiServer/yyai
vim serializers.py
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
vim views.py
from django.shortcuts import render
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from yyai import serializers #import UserSerializer, GroupSerializer
# Create your views here.
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = serializers.UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = serializers.GroupSerializer
4.2.初步启动及测试
python manage.py runserver 0.0.0.0:8099
Web端访问:
命令行访问:
curl -H 'Accept: application/json; indent=4' -u admin:admin123 http://192.168.**.**:8099/users/
4.3.模型环境部署
python3 -m pip install numpy
python3 -m pip install keras
python3 -m pip install tensorflow==1.9
python3 -m pip install -U ?pre numpy scipy matplotlib scikit-learn scikit-image
python3 -m pip install opencv_python
4.4.模型的加载和管理
vim urls.py
from django.conf.urls import url
from yyai import views,facescore,predictsex
urlpatterns = [
url(r'facescore/$', facescore.predict_face_score),
url(r'facescore_loadmodel/$', facescore.loadModel),
url(r'predictsex/$',predictsex.predict_sex),
url(r'predictsex_loadmodel/$',predictsex.loadModel),
]
vim modelUtils.py
import keras
from keras.models import load_model
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from scipy.misc import imresize
import numpy as np
import cv2
from