简体   繁体   English

Django React Python Post 请求图像分类

[英]Django React Python Post Request Image Classification

def post(self, request, *args, **kwargs):
    plants_serializer = PlantSerializer(data=request.data)
    data = request.FILES['plantImage']
    path = os.path.abspath('../PlantPharmacy/media/images/ ')
    path = path.strip()
    filename = path + str(data)

    if plants_serializer.is_valid():
        plants_serializer.save()
        result = predict(filename)
        obj = Plants.objects.get(plantImage = filename)
        obj.classification = result
        obj.save()
        return Response(plants_serializer.data, status=status.HTTP_201_CREATED)
    else:
        print('error', plants_serializer.errors)
        return Response(plants_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Hi!你好!

So I'm trying to edit a Django database model entry (setting its "classification" field as result gotten through a prediction function), but when I search for the image (Plants.objects.get(plantImage = filename) it says file does not exist, even though I have saved it. I have a feeling that its because it takes a moment for the database to recognize the file. I am trying to find a way to work around this because I need to return the image classification, but I am unsure to do so or a way to work around it.因此,我正在尝试编辑 Django 数据库模型条目(将其“分类”字段设置为通过预测函数获得的结果),但是当我搜索图像 (Plants.objects.get(plantImage = filename) 时,它说 file does不存在,即使我已经保存了它。我有一种感觉,因为数据库需要一些时间来识别文件。我试图找到一种方法来解决这个问题,因为我需要返回图像分类,但是我不确定这样做或解决它的方法。

get a "DoesNotExist( "%s matching query does not exist."" error.得到一个 "DoesNotExist( "%s 匹配查询不存在。"" 错误。

Try:尝试:

obj = plants_serializer.save(commit=False)
obj.classification = predict(filename)
obj.save()

I tried我试过

            plants_serializer.save()
            result = predict(filename)
            plants_serializer.save(user=request.user, classification=result)
            return Response(plants_serializer.data, status=status.HTTP_201_CREATED)

but got a "django.db.utils.IntegrityError: NOT NULL constraint failed:" error.但得到了一个“django.db.utils.IntegrityError:NOT NULL 约束失败:”错误。 I need to plants_serializer.save() before processing it, but then want to add the classification to the field in the existing entry in the DB.我需要在处理它之前进行plants_serializer.save(),但随后想将分类添加到数据库中现有条目的字段中。

Can this be done with one POST request/do i need to somehow GET the entry again before doing this?这可以通过一个 POST 请求完成吗/我是否需要在执行此操作之前以某种方式再次获取条目? I'm not super familiar with backend.我对后端不是很熟悉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM