Spaces:
Runtime error
Runtime error
Upload img_classification.py
Browse files- img_classification.py +125 -0
img_classification.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import warnings
|
5 |
+
warnings.filterwarnings("ignore")
|
6 |
+
|
7 |
+
|
8 |
+
class_names = ['apple_pie',
|
9 |
+
'baby_back_ribs',
|
10 |
+
'baklava',
|
11 |
+
'beef_carpaccio',
|
12 |
+
'beef_tartare',
|
13 |
+
'beet_salad',
|
14 |
+
'beignets',
|
15 |
+
'bibimbap',
|
16 |
+
'bread_pudding',
|
17 |
+
'breakfast_burrito',
|
18 |
+
'bruschetta',
|
19 |
+
'caesar_salad',
|
20 |
+
'cannoli',
|
21 |
+
'caprese_salad',
|
22 |
+
'carrot_cake',
|
23 |
+
'ceviche',
|
24 |
+
'cheesecake',
|
25 |
+
'cheese_plate',
|
26 |
+
'chicken_curry',
|
27 |
+
'chicken_quesadilla',
|
28 |
+
'chicken_wings',
|
29 |
+
'chocolate_cake',
|
30 |
+
'chocolate_mousse',
|
31 |
+
'churros',
|
32 |
+
'clam_chowder',
|
33 |
+
'club_sandwich',
|
34 |
+
'crab_cakes',
|
35 |
+
'creme_brulee',
|
36 |
+
'croque_madame',
|
37 |
+
'cup_cakes',
|
38 |
+
'deviled_eggs',
|
39 |
+
'donuts',
|
40 |
+
'dumplings',
|
41 |
+
'edamame',
|
42 |
+
'eggs_benedict',
|
43 |
+
'escargots',
|
44 |
+
'falafel',
|
45 |
+
'filet_mignon',
|
46 |
+
'fish_and_chips',
|
47 |
+
'foie_gras',
|
48 |
+
'french_fries',
|
49 |
+
'french_onion_soup',
|
50 |
+
'french_toast',
|
51 |
+
'fried_calamari',
|
52 |
+
'fried_rice',
|
53 |
+
'frozen_yogurt',
|
54 |
+
'garlic_bread',
|
55 |
+
'gnocchi',
|
56 |
+
'greek_salad',
|
57 |
+
'grilled_cheese_sandwich',
|
58 |
+
'grilled_salmon',
|
59 |
+
'guacamole',
|
60 |
+
'gyoza',
|
61 |
+
'hamburger',
|
62 |
+
'hot_and_sour_soup',
|
63 |
+
'hot_dog',
|
64 |
+
'huevos_rancheros',
|
65 |
+
'hummus',
|
66 |
+
'ice_cream',
|
67 |
+
'lasagna',
|
68 |
+
'lobster_bisque',
|
69 |
+
'lobster_roll_sandwich',
|
70 |
+
'macaroni_and_cheese',
|
71 |
+
'macarons',
|
72 |
+
'miso_soup',
|
73 |
+
'mussels',
|
74 |
+
'nachos',
|
75 |
+
'omelette',
|
76 |
+
'onion_rings',
|
77 |
+
'oysters',
|
78 |
+
'pad_thai',
|
79 |
+
'paella',
|
80 |
+
'pancakes',
|
81 |
+
'panna_cotta',
|
82 |
+
'peking_duck',
|
83 |
+
'pho',
|
84 |
+
'pizza',
|
85 |
+
'pork_chop',
|
86 |
+
'poutine',
|
87 |
+
'prime_rib',
|
88 |
+
'pulled_pork_sandwich',
|
89 |
+
'ramen',
|
90 |
+
'ravioli',
|
91 |
+
'red_velvet_cake',
|
92 |
+
'risotto',
|
93 |
+
'samosa',
|
94 |
+
'sashimi',
|
95 |
+
'scallops',
|
96 |
+
'seaweed_salad',
|
97 |
+
'shrimp_and_grits',
|
98 |
+
'spaghetti_bolognese',
|
99 |
+
'spaghetti_carbonara',
|
100 |
+
'spring_rolls',
|
101 |
+
'steak',
|
102 |
+
'strawberry_shortcake',
|
103 |
+
'sushi',
|
104 |
+
'tacos',
|
105 |
+
'takoyaki',
|
106 |
+
'tiramisu',
|
107 |
+
'tuna_tartare',
|
108 |
+
'waffles']
|
109 |
+
|
110 |
+
|
111 |
+
def load_and_prep_image(filename, img_shape=224, scale = True):
|
112 |
+
img = tf.io.read_file(filename)
|
113 |
+
img = tf.io.decode_image(img)
|
114 |
+
img = tf.image.resize(img, [img_shape, img_shape])
|
115 |
+
if scale:
|
116 |
+
return img/255.
|
117 |
+
else:
|
118 |
+
return img
|
119 |
+
|
120 |
+
model = tf.keras.models.load_model('converted_model.h5')
|
121 |
+
|
122 |
+
def classify(img):
|
123 |
+
pred_prob = model.predict(tf.expand_dims(img, axis=0))
|
124 |
+
pred_class = class_names[pred_prob.argmax()]
|
125 |
+
return pred_class
|