File size: 1,408 Bytes
6246f76
 
 
 
 
 
 
 
3d5d1af
f06fbd9
 
 
aa0c1c7
 
f06fbd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa0c1c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
license: apache-2.0
metrics:
- accuracy
pipeline_tag: image-classification
tags:
- MobileNetV2
- accident-detection
library_name: transformers
---

An image classification model for detecting car crashes from traffic cams. An easier to run version of Crashly is currently in development. To run this model, use the following code snippet.
```
import numpy as np
from PIL import Image
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="{model_name}.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']

# Load and preprocess image
def load_image(image_path):
    img = Image.open(image_path).convert('RGB')
    img = img.resize([input_shape[1], input_shape[2]])

    img = np.asarray(img, dtype='float32') / 255
    # Return a scaled array between -1 and 1
    return img * 2 - 1

if __name__ == "__main__":
    input_data =  load_image("/tmp/your-image-here.jpg")
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()

    # The function `get_tensor()` returns a copy of the tensor data.
    # Use `tensor()` in order to get a pointer to the tensor.
    output_data = interpreter.get_tensor(output_details[0]['index'])
    print(output_data)
```