如何在Django RestFramework上使用ML模型?

时间:2019-01-31 12:03:04

标签: machine-learning django-models django-rest-framework django-views

这基本上是我的整个项目。我已经准备好了django服务器和模型。我需要知道如何集成它,这样我的项目流程才能像这样: a)用户在Android应用上拍摄或上传图片。 b)拍摄的图像作为编码的64位图像字符串进入我服务器上的Android表。 c)然后由ML模型处理图像,图像将结果(将是标签)发送回去,以便人们可以查看盘子的标签。 我朝着正确的方向前进吗?如果没有,我该怎么办? 需要帮助。谢谢。

当前设置可以正常工作,但现在却出现错误:

参数应该是类似字节的对象或ASCII字符串,而不是“ ListSerializer”

Models.py

class Food(models.Model):
label=models.TextField(max_length=200)
link = models.FileField(null=False, blank=False)
 Image = models.TextField(link, null=True)

class Android(models.Model):
label=models.TextField(max_length=200)
imagestring=models.TextField(null=True,blank=True)  //convert this to Image

Serializer.py

from rest_framework import serializers
from .models import Server,Android
from api import Code
from  habibi import  readb64 as decode
from  habibi import  main as m

class AndroidSerializers(serializers.HyperlinkedModelSerializer):
return_image = serializers.SerializerMethodField()
class Meta:
    model = Android
    fields = ('label', 'imagestring','return_image')

def get_return_image(self, value):
    queryset = Android.objects.filter(imagestring=value.imagestring)
    queryset_serializer = AndroidSerializers(queryset, many=True, read_only=True)
    a = decode(queryset_serializer)
    b = m(a)
    c = Code.predicet(b)
    return c


class FoodSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
    model=Server
    fields=('url','label','Image','link')

观看次数

class FoodViewSet(viewsets.ModelViewSet):
queryset = Server.objects.all()
serializer_class =FoodSerializers


class Androids(viewsets.ModelViewSet):
queryset =Android.objects.all()
serializer_class = AndroidSerializers

Code.py(预测)

  from sklearn import preprocessing
  import numpy as np 
  from sklearn.externals import joblib
  def predicet(b):
 seed = 7
 np.random.seed(seed)

Y = ['Club Sandwich', 'Onion Ring', 'Pizza']

encodedvalue = preprocessing.LabelEncoder()
encoded_y = encodedvalue.fit_transform(Y)
clf = joblib.load('D:\csvs\models\pridictionmodel.pkl')
y_pred = clf.predict(b)
haaha = encodedvalue.inverse_transform(y_pred)
return  haaha

Habibi.py(功能)

import cv2
import math as m
import base64
from PIL import Image
import cv2
from io import StringIO
import numpy as np



def Applysift(img):
sift = cv2.xfeatures2d.SIFT_create()
kp, des = sift.detectAndCompute(img, None)
x = np.array(des)

y = x[:500, :]

j = y.flatten()


return j



 def GetIntensity(img):
  h, w, c = np.shape(img)
  height=h-2
  widht=w-2
  blue = img.item(height, widht, 0)
  green = img.item(height, widht, 1)
 red = img.item(height, widht, 2)
 inte=((0.2126*red) + (0.7152*green) + (0.0722*blue))

 uper = m.ceil(inte)

 return uper



 def average_hash(image, hash_size=8):

 im_pil = Image.fromarray(image)
 img = im_pil.convert('L').resize((hash_size, hash_size),Image.ANTIALIAS)
 pixels = np.array(img.getdata()).reshape((hash_size, hash_size))
 avg = pixels.mean()
 diff = pixels > avg
 // make a hash
 return diff



def binary_array_to_hex(arr):
h = 0
s = []
for i,v in enumerate(arr.flatten()):
    if v: h += 2**(i % 8)
    if (i % 8) == 7:
        s.append(hex(h)[2:].rjust(2, '0'))
        h = 0
return "".join(s)


  def hex_to_int(hexval):
  return int(hexval,16)





def readb64(base64_string):
sbuf = StringIO()
sbuf.write(base64.b64decode(base64_string))
pimg = Image.open(sbuf)
return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)


def main(a):
#links of image

j=a
#img1 = cv2.imread(j)
img = cv2.imread(j, -1)
sift = Applysift(img)
intensity = GetIntensity(img)
hash = average_hash(img)
arr = hash * 1
hexval = binary_array_to_hex(arr)
intval = hex_to_int(hexval)
orb_List = np.array(sift).tolist()
orb_List.insert(0, intensity)
orb_List.insert(1, intval)
feature = np.array(orb_List)

#df = pd.DataFrame(feature)
#dff = df.T
return feature

0 个答案:

没有答案