ccwu0918 commited on
Commit
08e867a
ยท
1 Parent(s): 3dbd2af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -11,65 +11,68 @@ from tensorflow.keras.utils import to_categorical
11
  from tensorflow.keras.applications.resnet_v2 import preprocess_input
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
 
 
14
  image_folders = ['King_Crab', 'Wind_Lion_God', 'pavo_cristatus', 'otter', 'Upupa_epops']
 
 
15
  labels = ["้ฑŸ", "้‡‘้–€้ขจ็…็ˆบ", "้‡‘้–€่—ๅญ”้›€", "ๆญไบžๆฐด็บ", "้‡‘้–€ๆˆดๅ‹้ณฅ"]
16
 
17
- base_dir = './'
18
 
19
- thedir = base_dir + image_folders[0]
20
- os.listdir(thedir)
21
 
22
- data = []
23
- target = []
24
- for i in range(5):
25
- thedir = base_dir + image_folders[i]
26
- image_fnames = os.listdir(thedir)
27
- for theimage in image_fnames:
28
- if theimage == ".git" or theimage == ".ipynb_checkpoints":
29
- continue
30
- img_path = thedir + '/' + theimage
31
- img = load_img(img_path , target_size = (256,256))
32
- x = img_to_array(img)
33
- data.append(x)
34
- target.append(i)
35
 
36
- model = load_model('my_cnn_model.pb') # Loading the Tensorflow Saved Model (PB)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  print(model.summary())
38
 
 
 
39
  def classify_image(inp):
40
  inp = inp.reshape((-1, 256, 256, 3))
41
  inp = preprocess_input(inp)
42
  prediction = model.predict(inp).flatten()
43
  return {labels[i]: float(prediction[i]) for i in range(5)}
44
 
45
- image = gr.Image(shape=(256, 256), label="้‡‘้–€่—ๅญ”้›€ใ€ๆญไบžๆฐด็บใ€ๆˆดๅ‹้ณฅ็…ง็‰‡")
46
- label = gr.Label(num_top_classes=5, label="AI่พจ่ญ˜็ตๆžœ")
47
- some_text="ๆˆ‘่ƒฝ่พจ่ญ˜้‡‘้–€่—ๅญ”้›€ใ€ๆญไบžๆฐด็บใ€ๆˆดๅ‹้ณฅใ€‚ๆ‰พๅผต้‡‘้–€่—ๅญ”้›€ใ€ๆญไบžๆฐด็บใ€ๆˆดๅ‹้ณฅ็…ง็‰‡ไพ†่€ƒๆˆ‘ๅง!"
 
 
48
 
49
  sample_images = []
50
- for i in range(5):
51
  thedir = base_dir + image_folders[i]
52
  for file in os.listdir(thedir):
53
  if file == ".git" or file == ".ipynb_checkpoints":
54
  continue
55
- sample_images.append(image_folders[i] + '/' + file)
56
 
 
 
57
  iface = gr.Interface(fn=classify_image,
58
  inputs=image,
59
  outputs=label,
60
- title="AI ้‡‘้–€่—ๅญ”้›€ใ€ๆญไบžๆฐด็บใ€ๆˆดๅ‹้ณฅ่พจ่ญ˜ๆฉŸ",
61
  description=some_text,
62
  examples=sample_images, live=True)
63
 
64
- # def greet(name):
65
-
66
- # model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
67
-
68
-
69
- # return "Hello " + name + "!!" + model.summary()
70
-
71
-
72
- # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
73
-
74
- # .launch(share=True)
75
  iface.launch()
 
11
  from tensorflow.keras.applications.resnet_v2 import preprocess_input
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
14
+ # ้‡‘้–€ๅ…ทๆœ‰ไปฃ่กจๆ€ง็š„ๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บไบ”็จฎ็‰ฉ็จฎใ€‚ๆˆ‘ๅ€‘ไพ†ๆŒ‘ๆˆฐไบ”็จฎ้กžๅˆฅ็ธฝๅ…ฑ็”จไบ”ๅๅผต็…ง็‰‡, ็œ‹่ƒฝไธ่ƒฝๆ‰“้€ ไธ€ๅ€‹็ฅž็ถ“็ถฒ่ทฏๅญธๆœƒ่พจ่ญ˜้€™ไบ”็จฎ้กžๅˆฅใ€‚
15
+ # ่ฎ€ๅ…ฅๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บ่ณ‡ๆ–™ๅœ–ๆช”
16
  image_folders = ['King_Crab', 'Wind_Lion_God', 'pavo_cristatus', 'otter', 'Upupa_epops']
17
+
18
+ # ็‚บไบ†ๅพŒ้ข็š„้œ€่ฆ๏ผŒๆˆ‘ๅ€‘ๅฐ‡ไบ”็จฎ้กžๅˆฅ็…ง็‰‡็š„็ญ”ๆกˆ็”จ `labels` ๅ‘ˆ็พ
19
  labels = ["้ฑŸ", "้‡‘้–€้ขจ็…็ˆบ", "้‡‘้–€่—ๅญ”้›€", "ๆญไบžๆฐด็บ", "้‡‘้–€ๆˆดๅ‹้ณฅ"]
20
 
21
+ num_classes = len(labels)
22
 
23
+ base_dir = './classify_image/'
 
24
 
25
+ # thedir = base_dir + image_folders[0]
26
+ # os.listdir(thedir)
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # data = []
29
+ # target = []
30
+ # for i in range(5):
31
+ # thedir = base_dir + image_folders[i]
32
+ # image_fnames = os.listdir(thedir)
33
+ # for theimage in image_fnames:
34
+ # if theimage == ".git" or theimage == ".ipynb_checkpoints":
35
+ # continue
36
+ # img_path = thedir + '/' + theimage
37
+ # img = load_img(img_path , target_size = (256,256))
38
+ # x = img_to_array(img)
39
+ # data.append(x)
40
+ # target.append(i)
41
+
42
+ # ่ผ‰ๅ…ฅไธฆๆชข่ฆ–่จ“็ทดๅฎŒๆˆ็š„ๆจกๅž‹ใ€‚
43
+
44
+ model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
45
  print(model.summary())
46
 
47
+ # ๆณจๆ„็พๅœจไธปๅ‡ฝๆ•ธๅš่พจ่ญ˜ๅชๆœ‰ไบ”ๅ€‹็จฎ้กžใ€‚่€Œไธ”ๆ˜ฏไฝฟ็”จๆˆ‘ๅ€‘่‡ช่กŒ่จ“็ทด็š„ model!
48
+
49
  def classify_image(inp):
50
  inp = inp.reshape((-1, 256, 256, 3))
51
  inp = preprocess_input(inp)
52
  prediction = model.predict(inp).flatten()
53
  return {labels[i]: float(prediction[i]) for i in range(5)}
54
 
55
+ image = gr.Image(shape=(256, 256), label="ๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บ็…ง็‰‡")
56
+ label = gr.Label(num_top_classes=num_classes, label="AI ResNet50V2้ท็งปๅผๅญธ็ฟ’่พจ่ญ˜็ตๆžœ")
57
+ some_text="ๆˆ‘่ƒฝ่พจ่ญ˜้‡‘้–€ๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บใ€‚ๆ‰พๅผต้‡‘้–€ๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บ็…ง็‰‡ไพ†่€ƒๆˆ‘ๅง!"
58
+
59
+ # ๆˆ‘ๅ€‘ๅฐ‡้‡‘้–€ๆ —ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บๆ•ธๆ“šๅบซไธญ็š„ๅœ–็‰‡ๆ‹ฟๅ‡บไพ†็•ถไฝœ็ฏ„ไพ‹ๅœ–็‰‡่ฎ“ไฝฟ็”จ่€…ไฝฟ็”จ
60
 
61
  sample_images = []
62
+ for i in range(num_classes):
63
  thedir = base_dir + image_folders[i]
64
  for file in os.listdir(thedir):
65
  if file == ".git" or file == ".ipynb_checkpoints":
66
  continue
67
+ sample_images.append(base_dir + image_folders[i] + '/' + file)
68
 
69
+ # ๆœ€ๅพŒ๏ผŒๅฐ‡ๆ‰€ๆœ‰ๆฑ่ฅฟ็ต„่ฃๅœจไธ€่ตท๏ผŒๅฐฑๅคงๅŠŸๅ‘Šๆˆไบ†๏ผ
70
+
71
  iface = gr.Interface(fn=classify_image,
72
  inputs=image,
73
  outputs=label,
74
+ title="AI ๅ–‰่œ‚่™Žใ€่—ๅญ”้›€ใ€ๆˆดๅ‹ใ€้ฑŸๅŠๆญไบžๆฐด็บ่พจ่ญ˜ๆฉŸ"",
75
  description=some_text,
76
  examples=sample_images, live=True)
77
 
 
 
 
 
 
 
 
 
 
 
 
78
  iface.launch()