-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_save
More file actions
125 lines (125 loc) · 4.44 KB
/
Copy pathpredict_save
File metadata and controls
125 lines (125 loc) · 4.44 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyN39wo1GCbWb3JtEhh1Jmzk"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "EDTnYiGtWLv0"
},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import load_model\n",
"from PIL import Image\n",
"import time"
]
},
{
"cell_type": "code",
"source": [
"!curl -LO https://github.com/MrTimonM/cap-dataset/releases/download/v1.11/Images.zip\n",
"!unzip -qq Images.zip\n",
"!curl -LO https://github.com/MrTimonM/cap-dataset/releases/download/v1.11/saved_model.zip\n",
"!unzip saved_model.zip -d saved_model"
],
"metadata": {
"id": "K_mvf-dDWZT2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import os\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import load_model\n",
"from PIL import Image\n",
"import time\n",
"\n",
"# Load the saved model\n",
"model = load_model('saved_model')\n",
"\n",
"# Define the directory where the images are located\n",
"image_dir = 'Images'\n",
"predicted_dir = 'predicted'\n",
"\n",
"# Create the predicted directory if it doesn't exist\n",
"if not os.path.exists(predicted_dir):\n",
" os.makedirs(predicted_dir)\n",
"\n",
"# Function to parse label from filename\n",
"def parse_label(filename):\n",
" return filename.split('.')[0] # Assuming label is the part before the first '.'\n",
"\n",
"# Function to format the predicted label\n",
"def format_predicted_label(prediction):\n",
" return ''.join(map(lambda x: chr(np.argmax(x)), prediction[0]))\n",
"\n",
"# Initialize total time taken\n",
"total_time = 0\n",
"\n",
"# Iterate through all images in the directory\n",
"for filename in os.listdir(image_dir):\n",
" filepath = os.path.join(image_dir, filename)\n",
" # Load and preprocess the image\n",
" image = Image.open(filepath)\n",
" image = image.resize((270, 130)) # Resize to match model input shape\n",
" image = np.array(image) / 255.0 # Normalize pixel values\n",
" image = np.expand_dims(image, axis=0) # Add batch dimension\n",
"\n",
" # Predict label for the image\n",
" start_time = time.time()\n",
" prediction = model.predict(image)\n",
" end_time = time.time()\n",
"\n",
" # Calculate time taken for prediction\n",
" time_taken = end_time - start_time\n",
" total_time += time_taken\n",
"\n",
" # Format predicted label\n",
" predicted_label = format_predicted_label(prediction)\n",
"\n",
" # Rename the image file with the predicted label and save it in the predicted directory\n",
" new_filename = predicted_label + '.png'\n",
" new_filepath = os.path.join(predicted_dir, new_filename)\n",
" # Convert the NumPy array back to a PIL image\n",
" image_to_save = Image.fromarray((image[0] * 255).astype(np.uint8))\n",
"\n",
" # Save the image with the predicted label as the filename in the predicted directory\n",
" new_filename = predicted_label + '.png'\n",
" new_filepath = os.path.join(predicted_dir, new_filename)\n",
" image_to_save.save(new_filepath)\n",
"\n",
" # Print prediction output and time taken\n",
" print(f\"Predicted label for {filename}: {predicted_label} - Time taken: {time_taken:.4f} seconds\")\n",
"\n",
"print(\"Prediction and renaming completed successfully.\")\n",
"print(f\"Total time taken for {len(os.listdir(image_dir))} images: {total_time:.4f} seconds\")\n"
],
"metadata": {
"id": "iTgD8z_EWjyM"
},
"execution_count": null,
"outputs": []
}
]
}