Fastapi: What is the efficient way to get images using FAST API?

Created on 5 Nov 2020  路  20Comments  路  Source: tiangolo/fastapi

How to get an image using Fast API ?

question

All 20 comments

Get an image of/from what?

```Python

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from pydantic import BaseModel

import numpy as np

from PIL import Image

import shutil

import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

app = FastAPI()

detect_fn = tf.saved_model.load('saved_model')
category_index = label_map_util.create_category_index_from_labelmap("label_map.pbtxt",use_display_name=True)

def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.

Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.

Args:
  path: the file path to the image

Returns:
  uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(path))

def predict(image):
# The input needs to be a tensor, convert it using tf.convert_to_tensor.
image_np = np.array(image.file)
input_tensor = tf.convert_to_tensor(image_np)
# The model expects a batch of images, so add an axis with tf.newaxis.
input_tensor = input_tensor[tf.newaxis, ...]

# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)

# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(detections.pop('num_detections'))

detections = {key: value[0, :num_detections].numpy()
               for key, value in detections.items()}
detections['num_detections'] = num_detections

# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

image_np_with_detections = image_np.copy()

viz_utils.visualize_boxes_and_labels_on_image_array(
      image_np_with_detections,
      detections['detection_boxes'],
      detections['detection_classes'],
      detections['detection_scores'],
      category_index,
      use_normalized_coordinates=True,
      max_boxes_to_draw=200,
      min_score_thresh=.50,
      agnostic_mode=False)
print("Detected")
return image_np_with_detections

@app.get("/")
async def main():
return FileResponse(r"misdo.jpg")

@app.post("/uploadfile/")
async def create_upload_file(image: UploadFile = File(...)):
predicted_image = predict(image)
return predicted_image```

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type SpooledTemporaryFile).

Get an image of/from what?

Could you look at my code and suggest me a way to pass image and get inference from it?

You can post an image using a form file upload, the rest is up to you.

See here: https://fastapi.tiangolo.com/tutorial/request-files/

In what format would the uploaded image/file be?

You can post an image using a form file upload, the rest is up to you.

See here: https://fastapi.tiangolo.com/tutorial/request-files/

In what format would the uploaded image/file be?

Btw in your code, you are trying to predict with a SpooledTemporaryFile not the file itself, use image.file instead.

Btw in your code, you are trying to predict with a SpooledTemporaryFile not the file itself

Inside the predict function I would have used image.file

@Gokulnath31 Ups,i must've missed it.

@Gokulnath31 the file will be in whatever format you post it? If you post a jpg, you'll get a jpg, etc

@Gokulnath31 the file will be in whatever format you post it? If you post a jpg, you'll get a jpg, etc

I found the answer there is a function called read using that I read the image content as bytes and converted it

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from pydantic import BaseModel

import io

import numpy as np

from PIL import Image

import shutil

import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

app = FastAPI()

detect_fn = tf.saved_model.load('saved_model')
category_index = label_map_util.create_category_index_from_labelmap("label_map.pbtxt",use_display_name=True)


def load_image_into_numpy_array(data):
    """Load an image from file into a numpy array.

    Puts image into numpy array to feed into tensorflow graph.
    Note that by convention we put it into a numpy array with shape
    (height, width, channels), where channels=3 for RGB.

    Args:
      path: the file path to the image

    Returns:
      uint8 numpy array with shape (img_height, img_width, 3)
    """
    return np.array(Image.open(io.BytesIO(data)))


def predict(image):
     # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
    image_np = load_image_into_numpy_array(image)
    input_tensor = tf.convert_to_tensor(image_np)
    print(input_tensor.shape)
    # The model expects a batch of images, so add an axis with `tf.newaxis`.
    input_tensor = input_tensor[tf.newaxis, ...]

    # input_tensor = np.expand_dims(image_np, 0)
    detections = detect_fn(input_tensor)

    # All outputs are batches tensors.
    # Convert to numpy arrays, and take index [0] to remove the batch dimension.
    # We're only interested in the first num_detections.
    num_detections = int(detections.pop('num_detections'))

    detections = {key: value[0, :num_detections].numpy()
                   for key, value in detections.items()}
    detections['num_detections'] = num_detections

    # detection_classes should be ints.
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

    image_np_with_detections = image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'],
          detections['detection_classes'],
          detections['detection_scores'],
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,
          min_score_thresh=.50,
          agnostic_mode=False)
    print("Detected")
    return image_np_with_detections


@app.get("/")
async def main():
    return FileResponse(r"misdo.jpg")


@app.post("/uploadfile/")
async def create_upload_file(image: UploadFile = File(...)):
    img_data = await image.read()
    predicted_image = predict(img_data)
    return predicted_image

But I am facing some other issues with loading cuda libraries

2020-11-05 13:45:52.639682: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:45:53.943552: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
2020-11-05 13:45:53.948468: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
INFO: 127.0.0.1:64114 - "POST /uploadfile/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 389, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\applications.py", line 179, in __call__
await super().__call__(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 227, in handle
await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 41, in app
response = await func(request)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 183, in app
dependant=dependant, values=values, is_coroutine=is_coroutine
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 133, in run_endpoint_function
return await dependant.call(
values)
File ".\api.py", line 85, in create_upload_file
predicted_image = predict(img_data)
File ".\api.py", line 47, in predict
detections = detect_fn(input_tensor)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\saved_model\load.py", line 509, in _call_attribute
return instance.__call__(args, *kwargs)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(args, *kwds)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\def_function.py", line 846, in _call
return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds) # pylint: disable=protected-access
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\function.py", line 1848, in _filtered_call
cancellation_manager=cancellation_manager)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\function.py", line 1924, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\function.py", line 550, in call
ctx=ctx)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node StatefulPartitionedCall/ssd_mobile_net_v2fpn_keras_feature_extractor/functional_1/Conv1/Conv2D}}]]
[[StatefulPartitionedCall/Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Reshape/_64]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node StatefulPartitionedCall/ssd_mobile_net_v2fpn_keras_feature_extractor/functional_1/Conv1/Conv2D}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_restored_function_body_49628]

Function call stack:
restored_function_body -> restored_function_body

WARNING: Detected file change in 'api.py'. Reloading...
2020-11-05 13:48:29.841520: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:48:34.271709: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-11-05 13:48:35.486899: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2020-11-05 13:48:35.495410: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:48:35.508514: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-11-05 13:48:35.518288: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-11-05 13:48:35.523537: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-11-05 13:48:35.533288: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-11-05 13:48:35.542287: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-11-05 13:48:35.559898: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:48:35.564893: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-11-05 13:48:35.568591: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-05 13:48:35.584917: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x21b301b2d00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-05 13:48:35.589754: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-11-05 13:48:35.596274: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2020-11-05 13:48:35.604391: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:48:35.608076: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-11-05 13:48:35.611968: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-11-05 13:48:35.616459: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-11-05 13:48:35.620139: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-11-05 13:48:35.624158: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-11-05 13:48:35.629499: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:48:35.633295: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-11-05 13:48:36.284913: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-11-05 13:48:36.290404: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263] 0
2020-11-05 13:48:36.292787: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0: N
2020-11-05 13:48:36.295533: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2905 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-11-05 13:48:36.305660: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x21b52d976b0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-11-05 13:48:36.310961: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1650 Ti, Compute Capability 7.5
INFO: Started server process [2032]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:64415 - "POST /uploadfile/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 389, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\applications.py", line 179, in __call__
await super().__call__(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 227, in handle
await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 41, in app
response = await func(request)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 183, in app
dependant=dependant, values=values, is_coroutine=is_coroutine
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 133, in run_endpoint_function
return await dependant.call(values)
File ".\api.py", line 86, in create_upload_file
predicted_image = predict(img_data)
File ".\api.py", line 43, in predict
print(input_tensor.shape())
TypeError: 'TensorShape' object is not callable
INFO: 127.0.0.1:64423 - "POST /uploadfile/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 389, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\applications.py", line 179, in __call__
await super().__call__(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 227, in handle
await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 41, in app
response = await func(request)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 183, in app
dependant=dependant, values=values, is_coroutine=is_coroutine
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 133, in run_endpoint_function
return await dependant.call(
values)
File ".\api.py", line 86, in create_upload_file
predicted_image = predict(img_data)
File ".\api.py", line 43, in predict
print(input_tensor.shape())
TypeError: 'TensorShape' object is not callable
WARNING: Detected file change in 'api.py'. Reloading...
2020-11-05 13:49:20.335698: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:49:23.845101: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-11-05 13:49:24.761712: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2020-11-05 13:49:24.770216: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:49:24.778382: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-11-05 13:49:24.784437: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-11-05 13:49:24.791746: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-11-05 13:49:24.799000: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-11-05 13:49:24.806297: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-11-05 13:49:24.820214: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:49:24.824928: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-11-05 13:49:24.828401: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-05 13:49:24.849003: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x12ce6fd1a00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-05 13:49:24.855502: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-11-05 13:49:24.861839: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1650 Ti computeCapability: 7.5
coreClock: 1.485GHz coreCount: 16 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 178.84GiB/s
2020-11-05 13:49:24.872308: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-05 13:49:24.877183: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-11-05 13:49:24.881524: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-11-05 13:49:24.887515: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-11-05 13:49:24.891730: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-11-05 13:49:24.896201: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-11-05 13:49:24.899834: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:49:24.903427: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-11-05 13:49:25.510396: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-11-05 13:49:25.515865: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263] 0
2020-11-05 13:49:25.518655: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0: N
2020-11-05 13:49:25.522499: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2905 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-11-05 13:49:25.531706: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x12c887fcf70 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-11-05 13:49:25.537499: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1650 Ti, Compute Capability 7.5
INFO: Started server process [2024]
INFO: Waiting for application startup.
INFO: Application startup complete.
(288, 432, 3)
2020-11-05 13:50:25.225508: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-05 13:50:26.551697: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
2020-11-05 13:50:26.557905: E tensorflow/stream_executor/cuda/cuda_dnn.cc:328] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
INFO: 127.0.0.1:64459 - "POST /uploadfile/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
T**

It looks like this is your issue:

File ".\api.py", line 86, in create_upload_file
predicted_image = predict(img_data)
File ".\api.py", line 43, in predict
print(input_tensor.shape())
TypeError: 'TensorShape' object is not callable

It looks like this is your issue:

File ".\api.py", line 86, in create_upload_file
predicted_image = predict(img_data)
File ".\api.py", line 43, in predict
print(input_tensor.shape())
TypeError: 'TensorShape' object is not callable

I have one small doubt. In what format I can return an image as response?

You can return it as a jpg or png?

You can return it as a jpg or png?

Returning in such format shows internal server error

Show us the error so we can actually help you please

Show us the error so we can actually help you please

```Python
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from pydantic import BaseModel

import io

import numpy as np

from PIL import Image

import shutil
import os

os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

app = FastAPI()

detect_fn = tf.saved_model.load('saved_model')
category_index = label_map_util.create_category_index_from_labelmap("label_map.pbtxt",use_display_name=True)

def load_image_into_numpy_array(data):
"""Load an image from file into a numpy array.

Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.

Args:
  path: the file path to the image

Returns:
  uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(io.BytesIO(data)))

def predict(image):
# The input needs to be a tensor, convert it using tf.convert_to_tensor.
image_np = load_image_into_numpy_array(image)
input_tensor = tf.convert_to_tensor(image_np)
#print(input_tensor.shape)
# The model expects a batch of images, so add an axis with tf.newaxis.
input_tensor = input_tensor[tf.newaxis, ...]

# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)

# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(detections.pop('num_detections'))

detections = {key: value[0, :num_detections].numpy()
               for key, value in detections.items()}
detections['num_detections'] = num_detections

# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

image_np_with_detections = image_np.copy()

viz_utils.visualize_boxes_and_labels_on_image_array(
      image_np_with_detections,
      detections['detection_boxes'],
      detections['detection_classes'],
      detections['detection_scores'],
      category_index,
      use_normalized_coordinates=True,
      max_boxes_to_draw=200,
      min_score_thresh=.50,
      agnostic_mode=False)
#print(detections)
return Image.fromarray(image_np_with_detections)

@app.get("/")
async def main():
return FileResponse(r"misdo.jpg")

@app.post("/uploadfile/")
async def create_upload_file(image: UploadFile = File(...)):
img_data = await image.read()
predicted_image = predict(img_data)
#print(predicted_image.shape)
#predicted_image.save("Predicted.jpg")
return predicted_image
````

Traceback (most recent call last):
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 389, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\applications.py", line 179, in __call__
await super().__call__(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 227, in handle
await self.app(scope, receive, send)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\starlette\routing.py", line 41, in app
response = await func(request)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 199, in app
is_coroutine=is_coroutine,
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\routing.py", line 122, in serialize_response
return jsonable_encoder(response_content)
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\encoders.py", line 147, in jsonable_encoder
sqlalchemy_safe=sqlalchemy_safe,
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\encoders.py", line 94, in jsonable_encoder
sqlalchemy_safe=sqlalchemy_safe,
File "c:\users\goku\anaconda3\envs\tensorflow2.0\lib\site-packages\fastapi\encoders.py", line 139, in jsonable_encoder
raise ValueError(errors)
ValueError: [ValueError('dictionary update sequence element #0 has length 3; 2 is required'), TypeError('vars() argument must have __dict__ attribute')]

You must return a fastapi response with the image in a binary format, and with the correct mimetype

from io import BytesIO

output = BytesIO()
predicted_image.save(output, 'png')

return Response(output.getvalue(), media_type='image/png')

In the future, please consult the documentation, and read the error messages you receive.

https://fastapi.tiangolo.com/advanced/custom-response/#response

Was this page helpful?
0 / 5 - 0 ratings