не JSON сериализуемый - PullRequest
0 голосов
/ 28 мая 2020

Я построил модель CNN и хорошо классифицирую. Но я хочу попытаться использовать Django для классификации класса изображения, когда изображение передается по URL-адресу. Вот несколько вещей, которые я пробовал. функция прогнозирования в моем apps.py

def prediction(image_loc):
    image_path = 'D:/'
    image = cv2.imread(os.path.join(image_path,image_loc))
    print("image in matrix is ", image)
    output = image.copy()

    # Pre-Process the image for classification
    image = cv2.resize(image, (96, 96))
    image = image.astype("float") / 255.0
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)

    # Load the trained convolutional neural network and the label binarizer
    print("[INFO] Loading Model...")
    model_RP = load_model(os.path.join(os.getcwd(), "model\cnn_model_RP.hdf5"))
    lb_path = os.path.join(os.getcwd(),"model\labelbin_RP")
    lb_RP = pickle.loads(open(lb_path, "rb").read())
    print("[INFO] Model Loading Complete.")

    # Classify the input image
    print("[INFO] classifying image...")
    proba = model_RP.predict(image)[0]
    idx = np.argmax(proba)
    label = lb_RP.classes_[idx]

    #we will mark our prediction as "correct" of the input image filename contains the predicted label text 
    #(obviously this makes the assumption that you have named your testing image files this way)

    filename = image_loc[image_loc.rfind(os.path.sep) + 1:]
    correct = "correct" if filename.rfind(label) != -1 else "incorrect"

    # Build the label and draw the label on the image
    label = "{}: {:.2f}% ({})".format(label, proba[idx] * 100, correct)
    output = imutils.resize(output, width=400)
    cv2.putText(output, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)

    # show the output image
    print("[INFO] {}".format(label))
    #plt.imshow(output)
    #plt.show()

    #return(plt.show())
    return(label) #- WORKING

    #return HttpResponse(output, content_type = 'image/png')  
    #resp = HttpResponse("", content_type = 'image/png')
    #resp.write('output')
    #return resp

  1. Если я верну только метку, которая работает, ниже приведен фрагмент кода из моего apps.py
    label = "{}: {:.2f}% ({})".format(label, proba[idx] * 100, correct)
    return (label)

2.a Вот в чем проблема. Я также пытаюсь вернуть изображение с этикеткой на нем. что мне не удалось.

    label = "{}: {:.2f}% ({})".format(label, proba[idx] * 100, correct)
    output = imutils.resize(output, width=400)
    cv2.putText(output, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) 

    return HttpResponse(output, content_type = 'image/png')

2.b Вот второй подход для возврата изображения, к сожалению, не удалось.

     label = "{}: {:.2f}% ({})".format(label, proba[idx] * 100, correct)
     output = imutils.resize(output, width=400)
     cv2.putText(output, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)      
     resp = HttpResponse("", content_type = 'image/png')
     resp.write('output')
     return resp
Вот мой views.py
@api_view(['GET'])  # Decorator
def call_model(request):
    #if request.method == 'GET':

            # sentence is the query we want to get the prediction for
    params =  request.GET.get('image_loc')

            # predict method used to get the prediction
    resp = prediction(image_loc = params)

            # returning JSON response
    return Response(resp) 

Трассировка стека ошибки, когда я запускаю в браузере следующее.

http://127.0.0.1:8000/model/img?format=json&image_loc=bulbasaur_plush.png
TypeError at /model/img
<HttpResponse status_code=200, "image/png"> is not JSON serializable
Request Method: GET
Request URL:    http://127.0.0.1:8000/model/img?format=json&image_loc=bulbasaur_plush.png
Django Version: 2.2.12
Exception Type: TypeError
Exception Value:    
<HttpResponse status_code=200, "image/png"> is not JSON serializable
Exception Location: C:\Anaconda3\envs\tf35\lib\json\encoder.py in default, line 179
Python Executable:  C:\Anaconda3\envs\tf35\python.exe
Python Version: 3.5.5
Python Path:    
['D:\\Discern\\Term 3\\deploy',
 'C:\\Anaconda3\\envs\\tf35\\python35.zip',
 'C:\\Anaconda3\\envs\\tf35\\DLLs',
 'C:\\Anaconda3\\envs\\tf35\\lib',
 'C:\\Anaconda3\\envs\\tf35',
 'C:\\Users\\prade\\AppData\\Roaming\\Python\\Python35\\site-packages',
 'C:\\Anaconda3\\envs\\tf35\\lib\\site-packages',
 'C:\\Anaconda3\\envs\\tf35\\lib\\site-packages\\win32',
 'C:\\Anaconda3\\envs\\tf35\\lib\\site-packages\\win32\\lib',
 'C:\\Anaconda3\\envs\\tf35\\lib\\site-packages\\Pythonwin']
Server time:    Fri, 29 May 2020 04:35:18 +0000

Пытался посмотреть в похожих сообщениях, но они связаны с текстом, поэтому мне не удалось решить мою проблему.

Кто-нибудь может помочь, пожалуйста.

1 Ответ

1 голос
/ 29 мая 2020

Похоже, что resp - это объект HttpResponse (больше не могу сказать, вы не указали код для prediction).

Вы должны сделать что-то JSON сериализуемым внутри Response() конструктор (обычно dict или список, содержащий только строку / int / boolean).

(Кстати, декоратор @api_view для Django Rest Framework устарел, @action предпочтительнее с последней версией)

...