view-函数进行处理

from __future__ import unicode_literals

# Create your views here.





import json

from PIL import Image
from django.shortcuts import render

import os
# coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse

# 引入我们创建的表单类
from learn.models import Img
from .forms import AddForm

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.conf import settings

BASE_DIR = settings.BASE_DIR  # 项目目录
# 假设图片放在static/pics/里面
PICS = os.listdir(os.path.join(BASE_DIR, 'common_static/pics'))

print (PICS)

def index(request):
    return render(request, 'index.html')

def get_pic(request):
    color = request.GET.get('color')
    number = request.GET.get('number')
    name = '{}_{}'.format(color, number)

    # 过滤出符合要求的图片,假设是以输入的开头的都返回
   # result_list = filter(lambda x: x.startswith(name), PICS)
    result_list =  PICS
    print('result_list', result_list)

    return HttpResponse(
        json.dumps(result_list),
        content_type='application/json')


def add2(request, a1, b1):
    c = int(a1) + int(b1)
    return HttpResponse(str(c))

def addRedi(request,a,b):
    return  HttpResponseRedirect(reverse('add2', args=(a, b)))

def add(request):
    a = request.GET['a']
    b = request.GET['b']
    a = int(a)
    b = int(b)
    return HttpResponse(str(a + b))



def home(request):
    return render(request, 'index.html')

from django.http import JsonResponse

def catinfo(request):
    if request.method == "POST":
        f1 = request.FILES['pic1']
       # img_path = os.path.join("static/image/",f1.name)
        img_path = '%s/pic/%s' % (settings.MEDIA_ROOT, f1.name)
        read_img_path="/media/pic/"+f1.name
        print(f1.name)
        with open(img_path, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        print(read_img_path)
        ret = {'code': True, 'data':read_img_path}  # 'data': img_path 数据为图片的路径,
        import json
        return HttpResponse(json.dumps(ret))
    else:
        return HttpResponse("error")


def catinfo(request):
    if request.method == "POST":
        f1 = request.FILES['pic1']
       # img_path = os.path.join("static/image/",f1.name)
        img_path = '%s/pic/%s' % (settings.MEDIA_ROOT, f1.name)
        read_img_path="/media/pic/"+f1.name
        print(f1.name)
        with open(img_path, 'wb') as pic:
            for c in f1.chunks():
                pic.write(c)
        print(read_img_path)
        ret = {'code': True, 'data':read_img_path}  # 'data': img_path 数据为图片的路径,
        import json
        return HttpResponse(json.dumps(ret))
    else:
        return HttpResponse("error")


def detectImage(request):
    if request.method == "POST":
        if "imgurl" in request.POST:
            print("yes")
        print(request.POST)
       # img_path = os.path.join("static/image/",f1.name)
       # img_path = '%s/pic/%s' % (settings.MEDIA_ROOT, f1.name)
      #  print(type(f1))
        name="3.jpg"
        read_img_path="/media/pic/"+"3.jpg"
        print("detected"+name)
        print(read_img_path)
        ret = {'code': True, 'resultImg':read_img_path,'resultText':"年龄是30岁"}  # 'data': img_path 数据为图片的路径,
        import json
        return HttpResponse(json.dumps(ret))
    else:
        return HttpResponse("error")


def ajax_list(request):
    a =  list(range(100))
    return JsonResponse(a, safe=False)


def ajax_dict(request):
    name_dict = {'twz': 'Love python and Django', 'zqxt': 'I am teaching Django'}
    return JsonResponse(name_dict)



# Create your views here.


def uploadpic(request):
    return render(request, 'upload_pic.html')

首先点击button上传图片通过 js iframeSubmit() 调用前端展现, 展现的时候将两个图片的url全部设置,同时给隐形的input text设置url的地址,点击识别按钮,将inputtext(imgurl)中的url传给后端,后端根据url加载图片,进行识别,最后返回文件,以及json结果给前端

猜你喜欢

转载自blog.csdn.net/z471365897/article/details/81506757