


import unittest

from cer import CER


_lowerCAmelCase:	Union[str, Any]      							=					CER()




class 			lowercase_				(unittest.TestCase      ):





				def 	__UpperCamelCase						(							self)     ->   int:
											a__     						=['White House']
											a__     						=['white house']
											# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.18_18_18_18_18) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   Dict:
											a__     						=['were wolf']
											a__     						=['werewolf']
											# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.1_11_11_11) < 1e-6)

											a__     						=['werewolf']
											a__     						=['weae     wolf']
											# S = 1, D = 1, I = 0, N = 8, CER = 0.25
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.25) < 1e-6)

											# consecutive whitespaces case 1
											a__     						=['were wolf']
											a__     						=['were               wolf']
											# S = 0, D = 0, I = 0, N = 9, CER = 0
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.0) < 1e-6)

											# consecutive whitespaces case 2
											a__     						=['were   wolf']
											a__     						=['were               wolf']
											# S = 0, D = 0, I = 0, N = 9, CER = 0
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.0) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   Union[str, Any]:
											a__     						=['werewolf']
											a__     						=['weaewolf']
											# S = 1, D = 0, I = 0, N = 8, CER = 0.125
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.1_25) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   Dict:
											a__     						=['werewolf']
											a__     						=['wereawolf']
											# S = 0, D = 1, I = 0, N = 8, CER = 0.125
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.1_25) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   str:
											a__     						=['werewolf']
											a__     						=['wereolf']
											# S = 0, D = 0, I = 1, N = 8, CER = 0.125
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.1_25) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   Tuple:
											a__     						=['werewolf']
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertEqual(lowercase_					,     0.0)





				def 	__UpperCamelCase						(							self)     ->   Optional[Any]:
											a__     						=['werewolf', 'I am your father']
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertEqual(lowercase_					,     0.0)

											a__     						=['werewolf', 'I am your father', 'doge']
											a__     						=['werxwolf', 'I       am your father', 'doge']
											# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.03_57_14_28) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   List[str]:
											a__     						=['My hovercraft', 'is full of eels']
											a__     						=['My hovercraft is full', ' of eels']
											# S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
											# whitespace at the front of " of eels" will be strip during preporcessing
											# so need to insert 2 whitespaces
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_					,     concatenate_texts=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.07_14_28) < 1e-6)





				def 	__UpperCamelCase						(							self)     ->   List[str]:
											a__     						=['我能吞下玻璃而不伤身体']
											a__     						=[' 能吞虾玻璃而 不霜身体啦']
											# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.45_45_45_45_45) < 1e-6)

											a__     						=['我能吞下玻璃', '而不伤身体']
											a__     						=['我    能 吞 下 玻 璃', '而不伤身体']
											# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertTrue(abs(char_error_rate - 0.45_45_45_45_45_45) < 1e-6)

											a__     						=['我能吞下玻璃而不伤身体']
											a__     						=cer.compute(predictions=lowercase_					,     references=lowercase_)
											self.assertFalse(lowercase_					,     0.0)





				def 	__UpperCamelCase						(							self)     ->   List[Any]:
											a__     						=['']
											a__     						=['Hypothesis']
											with self.assertRaises(lowercase_):
																		cer.compute(predictions=lowercase_					,     references=lowercase_)


if __name__ == "__main__":
	unittest.main()








import numpy as np
from PIL import Image






def      _lowercase(				__a							:	np.array    ):
							a__						,    a__						,    a__     						=rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
							return 0.29_89 * r + 0.58_70 * g + 0.11_40 * b






def      _lowercase(				__a							:	np.array    ):
							return (gray > 127) & (gray <= 255)






def      _lowercase(				__a							:	np.array						,    __a							:	np.array    ):
							a__     						=np.zeros_like(__a    )
							a__     						=np.zeros(
							    (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)    )

							# Copy image to padded image
							a__     						=image

							# Iterate over image & apply kernel
							for x in range(image.shape[1]    ):
														for y in range(image.shape[0]    ):
																					a__     						=(
																					    kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
																					).sum()
																					a__     						=int(summation == 5    )
							return output


# kernel to be applied
_lowerCAmelCase:	Dict      							=					np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])

if __name__ == "__main__":
	# read original image
	_lowerCAmelCase:	Optional[int]      							=					np.array(Image.open(R'..\image_data\lena.jpg'))
	# Apply erosion operation to a binary image
	_lowerCAmelCase:	Any      							=					erosion(grayabinary(rgbagray(image)), structuring_element)
	# Save the output image
	_lowerCAmelCase:	Dict      							=					Image.fromarray(output).convert('RGB')
	pil_img.save('result_erosion.png')

