Skip to content

Instantly share code, notes, and snippets.

@Praveen76
Created July 29, 2020 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Praveen76/14c02bdcdf94fe74294b4087bbf36b48 to your computer and use it in GitHub Desktop.
Save Praveen76/14c02bdcdf94fe74294b4087bbf36b48 to your computer and use it in GitHub Desktop.
#Write a function to choose one image randomly from your dataset and predict using Trained model.
def show_image_with_predictions(df, threshold=0.6):
# choose a random image
row = df.sample()
filepath = row['fileName'].values[0]
print("filepath:", filepath)
# get all rows for this image
df2 = df[df['fileName'] == filepath]
im = np.array(Image.open(filepath))
print("im.shape:", im.shape)
# if there's a PNG it will have alpha channel
im = im[:,:,:3]
# plot true boxes
for idx, row in df2.iterrows():
box = [
row['xmin'],
row['ymin'],
row['xmax'],
row['ymax'],
]
print(box)
draw_box(im, box, color=(255, 0, 0))
### plot predictions ###
# get predictions
imp = preprocess_image(im)
imp, scale = resize_image(im)
boxes, scores, labels = model.predict_on_batch(
np.expand_dims(imp, axis=0)
)
# standardize box coordinates
boxes /= scale
# loop through each prediction for the input image
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can quit as soon
# as we see a score below threshold
if score < threshold:
break
box = box.astype(np.int32)
color = label_color(label)
draw_box(im, box, color=color)
class_name = label_map[label]
caption = f"{class_name} {score:.3f}"
draw_caption(im, box, caption)
score, label=score, label
plt.axis('off')
plt.imshow(im)
plt.show()
return score, label
plt.rcParams['figure.figsize'] = [20, 10]
#Feel free to change threshold as per your business requirement
score, label=show_image_with_predictions(data, threshold=0.6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment