Update README.md
Browse files
README.md
CHANGED
@@ -6,4 +6,40 @@ pipeline_tag: image-classification
|
|
6 |
tags:
|
7 |
- MobileNetV2
|
8 |
- accident-detection
|
9 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
tags:
|
7 |
- MobileNetV2
|
8 |
- accident-detection
|
9 |
+
---
|
10 |
+
|
11 |
+
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.
|
12 |
+
`import numpy as np
|
13 |
+
from PIL import Image
|
14 |
+
import tensorflow as tf
|
15 |
+
|
16 |
+
# Load TFLite model and allocate tensors.
|
17 |
+
interpreter = tf.lite.Interpreter(model_path="{model_name}.tflite")
|
18 |
+
interpreter.allocate_tensors()
|
19 |
+
|
20 |
+
# Get input and output tensors.
|
21 |
+
input_details = interpreter.get_input_details()
|
22 |
+
output_details = interpreter.get_output_details()
|
23 |
+
|
24 |
+
input_shape = input_details[0]['shape']
|
25 |
+
|
26 |
+
# Load and preprocess image
|
27 |
+
def load_image(image_path):
|
28 |
+
img = Image.open(image_path).convert('RGB')
|
29 |
+
img = img.resize([input_shape[1], input_shape[2]])
|
30 |
+
|
31 |
+
img = np.asarray(img, dtype='float32') / 255
|
32 |
+
# Return a scaled array between -1 and 1
|
33 |
+
return img * 2 - 1
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
input_data = load_image("/tmp/your-image-here.jpg")
|
37 |
+
interpreter.set_tensor(input_details[0]['index'], input_data)
|
38 |
+
|
39 |
+
interpreter.invoke()
|
40 |
+
|
41 |
+
# The function `get_tensor()` returns a copy of the tensor data.
|
42 |
+
# Use `tensor()` in order to get a pointer to the tensor.
|
43 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
44 |
+
print(output_data)
|
45 |
+
`
|