【创建虚拟环境】
1 # 1.执行命令mkvirtualenv -p python3 smart_api 2 3 # 2.在虚拟环境中装django 4 pip install django==4.2.13 5 6 # 3. 创建后端项目 7 1 使用命令 8 django-admin startproject 项目名 9 2 pycharm创建--指定刚刚创建的虚拟环境 10 -找到刚刚创建的虚拟环境,加入进来,创建项目即可 11 -路径找到 12 -1 重启机器 13 -2 把虚拟环境目录浅一些 14 -3 使用命令创建,使用pycharm打开--》还得指定解释器
变成smart_api1
【欢迎页面】
# 1 后端需要配合接口 welcome
-welcome表:图片,id,跳转地址。。。
-接口:welcome接口
1 模型表:models.py 2 3 from django.db import models 4 5 6 # Create your models here. 7 class Welcome(models.Model): 8 img = models.ImageField(upload_to='welcome', default='slash.png') 9 order = models.IntegerField() 10 link = models.CharField(max_length=32) 11 create_time = models.DateTimeField(auto_now=True) 12 is_delete = models.BooleanField(default=False) 13 14 15 -------------------------------------------------------------------------------------- 16 views.py 17 18 from rest_framework.mixins import ListModelMixin 19 from rest_framework.viewsets import GenericViewSet 20 from core.models import Welcome 21 from core.serializers import WelcomeSerialzier 22 23 24 # Create your views here. 25 class WelcomeView(GenericViewSet, ListModelMixin): 26 queryset = Welcome.objects.all().filter(is_delete=False).order_by('-order') 27 serializer_class = WelcomeSerialzier 28 29 ------------------------------------------------------------------------------------- 30 serializer.py 31 32 from rest_framework import serializers 33 34 from core.models import Welcome 35 36 37 class WelcomeSerialzier(serializers.ModelSerializer): 38 class Meta: 39 model = Welcome 40 fields = ['img', 'link'] 41 42 --------------------------------------------------------------------------------------- 43 urls.py 44 45 from .views import WelcomeView 46 from rest_framework.routers import SimpleRouter 47 48 router = SimpleRouter() 49 router.register('welcome', WelcomeView,'welcome') 50 51 urlpatterns = [ 52 53 ] 54 55 urlpatterns += router.urls 56 57 -------------------------------------------------------------------------------------- 58 settings.py 59 60 MEDIA_URL = '/media/' 61 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
首页轮播图和公告接口
# 1 后端创建轮播图表和公告表
# 2 admin中录入数据
-simpleui
1 models.py 2 3 # 轮播图表 4 class Banner(models.Model): 5 img = models.ImageField(upload_to='banner', default='banner1.png', verbose_name='图片') 6 order = models.IntegerField(verbose_name='顺序') 7 create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间') 8 is_delete = models.BooleanField(default=False, verbose_name='是否删除') 9 10 class Meta: 11 verbose_name_plural = '轮播图' 12 13 14 # 公告表 15 class Notice(models.Model): 16 title = models.CharField(max_length=64, verbose_name='公共标题') 17 content = models.TextField(verbose_name='内容') 18 img = models.ImageField(upload_to='notice', default='notice.png', verbose_name='公告图片') 19 create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间') 20 21 class Meta: 22 verbose_name_plural = '公告表' 23 24 25 -------------------------------------------------------------------------------------- 26 views.py 27 28 29 # 轮播图接口 30 class BannerView(GenericViewSet, ListModelMixin): 31 queryset = Banner.objects.all().filter(is_delete=False).order_by('order')[:2] 32 serializer_class = BannerSerializer 33 34 def list(self, request, *args, **kwargs): 35 res = super().list(request, *args, **kwargs) 36 notice = Notice.objects.all().order_by('create_time').first() 37 serializer = NoticeSerializer(instance=notice) 38 return Response({'code': 100, 'msg': '成功', 'banner': res.data, 'notice': serializer.data}) 39 40 -------------------------------------------------------------------------------------- 41 serializer.py 42 43 # 轮播图表序列化类 44 class BannerSerializer(serializers.ModelSerializer): 45 class Meta: 46 model = Banner 47 fields = '__all__' 48 49 50 # 社区通知序列化类 51 class NoticeSerializer(serializers.ModelSerializer): 52 class Meta: 53 model = Notice 54 fields = ['id', 'title','content'] 55 56 ----------------------------------------------------------------------------------- 57 urls.py 58 59 router.register('banner', BannerView,'banner')
pip install django-simpleui
信息采集拍照上传
# 1 小程序端拍照---》上传到咱们后台--》后台保存一份(media)--》把图片---》传到百度ai人脸识别库中存着
# 2 学习
-百度ai人脸库上传
-百度ai人脸库删除
-百度ai人脸库根据人脸匹配
pip3 install baidu-aip pip3 install pypinyin pip install chardet 需要安装的模块
1 # 人脸注册 2 3 from aip import AipFace 4 import base64 5 from pypinyin import lazy_pinyin, Style 6 7 8 class BaiDuAI: 9 def __init__(self, APP_ID='76543972', API_KEY='LAJoFtfUTgOfF90UqAtbObq1', 10 SECRET_KEY='2QXO7MOA4uWdAOhjl1Vz4FMeC7xZtL4K'): 11 """ 你的 APPID AK SK """ 12 self.APP_ID = APP_ID 13 self.API_KEY = API_KEY 14 self.SECRET_KEY = SECRET_KEY 15 self.client = AipFace(self.APP_ID, self.API_KEY, self.SECRET_KEY) 16 17 def name_to_pinyin(self, text): 18 style = Style.TONE3 19 name_list = lazy_pinyin(text, style=style) 20 return ''.join(name_list) 21 22 def add_user(self): 23 image = base64.b64encode( 24 open(r'C:\Users\靳小洁\PycharmProjects\smart_api1\scripts\彭于晏.png', 'rb').read()).decode('utf-8') 25 imageType = "BASE64" 26 groupId = "100" 27 userId = self.name_to_pinyin('彭于晏') 28 """ 调用人脸注册 """ 29 res = self.client.addUser(image, imageType, groupId, userId) 30 print(res) 31 32 """ 如果有可选参数 """ 33 ''' 34 {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 937906163, 'timestamp': 1716948937, 'cached': 0, 'result': {'face_token': '7598884f9d9a349218941e8e6f52c884', 'location': {'left': 493.4, 'top': 329.74, 'width': 348, 'height': 321, 'rotation': -8}}} 35 ''' 36 37 def search(self): 38 image = base64.b64encode(open('彭于晏.png', 'rb').read()).decode('utf-8') 39 imageType = "BASE64" 40 groupIdList = "100,2" 41 """ 调用人脸搜索 """ 42 res = self.client.search(image, imageType, groupIdList); 43 """ 如果有可选参数 """ 44 print(res) 45 ''' 46 {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1190062038, 'timestamp': 1716949190, 'cached': 0, 'result': {'face_token': 'bdbc6214eb18ccee1bf72d1f72f0c979', 'user_list': [{'group_id': '100', 'user_id': 'peng2yu2yan4', 'user_info': '', 'score': 14.822490692139}]}} 47 ''' 48 49 def delete(self): 50 userId = "peng2yu2yan4" 51 groupId = "100" 52 faceToken = "374e14ed028e8b3fabe2c18d1656b381" 53 """ 调用人脸删除 """ 54 res = self.client.faceDelete(userId, groupId, faceToken); 55 print(res) 56 ''' 57 {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1355696697, 'timestamp': 1716949355, 'cached': 0, 'result': None} 58 ''' 59 60 61 if __name__ == '__main__': 62 ai = BaiDuAI() 63 # ai.add_user() 64 # ai.search() 65 ai.delete()
后端接口
# 1 采集上传接口---》存本地--》存百度ai
# 2 查询今天采集数据--》1 当前用户(没实现) 2 当天采集的数据 3 总条数Collection.objects.all().filter(create_time__gte=datetime.now().date())
# 3 删除采集接口-->id号删除
标签:name,后端,models,微信,self,----,import,class,verbose From: https://www.cnblogs.com/liuliu1/p/18244700