Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. from keras.models import load_model
  2. import cv2
  3. import numpy as np
  4. import sys
  5. import re
  6.  
  7. IMAGE_SIZE = (256,256)
  8. def iou(y_true, y_pred):
  9. from keras import backend as K
  10. y_true = K.cast(K.greater(y_true, 0.5), dtype='float32')
  11. y_pred = K.cast(K.greater(y_pred, 0.5), dtype='float32')
  12. inter = K.sum(K.sum(K.squeeze(y_true * y_pred, axis=3), axis=2), axis=1)
  13. union = K.sum(K.sum(K.squeeze(K.clip(y_true + y_pred, 0, 1), axis=3), axis=2), axis=1)
  14. return K.mean((inter + K.epsilon()) / (union + K.epsilon()))
  15. def threshold_slow(T, image):
  16. # grab the image dimensions
  17. h = image.shape[0]
  18. w = image.shape[1]
  19.  
  20. # loop over the image, pixel by pixel
  21. for y in range(0, h):
  22. for x in range(0, w):
  23. # threshold the pixel
  24. image[y, x] = 255 if image[y, x] >= T else 0
  25.  
  26. # return the thresholded image
  27. return image
  28. model = load_model('model2.h5', custom_objects={'iou': iou})
  29. name = 1
  30. index = []
  31. while name < 156:
  32. if not cv2.imread('textlocalize/validation/Input/'+str(name)+'.jpg') is None :
  33. index.append(str(name))
  34. if not cv2.imread('textlocalize/validation/Input/0'+str(name)+'.jpg') is None:
  35. index.append("0"+str(name))
  36. if not cv2.imread('textlocalize/validation/Input/00'+str(name)+'.jpg') is None:
  37. index.append("00"+str(name))
  38. name += 1
  39. for start in index:
  40. print(start)
  41. test_im = cv2.imread('textlocalize/validation/Input/'+str(start)+'.jpg')
  42. if not test_im is None:
  43. true_size = test_im.shape
  44.  
  45. imshow_size = (512,round(true_size[0]*512/true_size[1]))
  46. # cv2.imshow('Input',cv2.resize(test_im, imshow_size))
  47.  
  48. test_im = cv2.cvtColor(test_im, cv2.COLOR_BGR2RGB)
  49. test_im = cv2.resize(test_im, (IMAGE_SIZE[1], IMAGE_SIZE[0]))
  50. test_im = test_im/255.
  51. test_im = np.expand_dims(test_im, axis=0)
  52. segmented = model.predict(test_im)
  53. #segmented = np.around(segmented)
  54. IMAGE_out = (true_size[1],true_size[0])
  55. # segmented = (segmented[0, :, :, 0]*255).astype('uint8')
  56. segmented30 = threshold_slow(0.3,segmented[0, :, :, 0])
  57. segmented40 = threshold_slow(0.4,segmented[0, :, :, 0])
  58. segmented50 = threshold_slow(0.5,segmented[0, :, :, 0])
  59. output30 = cv2.resize(segmented30, (IMAGE_out[0],IMAGE_out[1]))
  60. output40 = cv2.resize(segmented40, (IMAGE_out[0],IMAGE_out[1]))
  61. output50 = cv2.resize(segmented50, (IMAGE_out[0],IMAGE_out[1]))
  62. status = cv2.imwrite('textlocalize/testoutput30/'+str(start)+'.jpg',output30)
  63. status = cv2.imwrite('textlocalize/testoutput40/'+str(start)+'.jpg',output40)
  64. status = cv2.imwrite('textlocalize/testoutput50/'+str(start)+'.jpg',output50)
  65. name += 1
  66. # cv2.imshow('Output',cv2.resize(segmented, imshow_size))
  67. cv2.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement