изменно на процентное соотношение
This commit is contained in:
		
							parent
							
								
									b2a8f10092
								
							
						
					
					
						commit
						a93054997e
					
				
							
								
								
									
										83
									
								
								text2.py
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								text2.py
									
									
									
									
									
								
							| @ -7,50 +7,65 @@ pytesseract.pytesseract.tesseract_cmd = r'C:\Users\ASUS\Desktop\fishrungame\trea | ||||
| 
 | ||||
| 
 | ||||
| def draw_boxes(image, boxes, texts): | ||||
|     for i, box in enumerate(boxes): | ||||
|         x, y, x_w, y_h = box | ||||
|         cv2.rectangle(image, (x, y), (x + x_w, y + y_h), (0, 255, 0), 2) | ||||
|         cv2.putText(image, texts[i], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) | ||||
|         for i, box in enumerate(boxes): | ||||
|             x, y, x_w, y_h = box | ||||
|             cv2.rectangle(image, (x, y), (x + x_w, y + y_h), (0, 255, 0), 2) | ||||
|             cv2.putText(image, texts[i], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) | ||||
| 
 | ||||
| 
 | ||||
| def process_image(image_path, coordinates_list): | ||||
|     image = cv2.imread(image_path) | ||||
|     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | ||||
| def process_image(image_path, percentage_coordinates_list): | ||||
|         # Загрузка изображения и изменение размера | ||||
|         original_image = cv2.imread(image_path) | ||||
|         resized_image = cv2.resize(original_image, (662, 429)) | ||||
| 
 | ||||
|     # Применение OCR | ||||
|     custom_config = r'--oem 3 --psm 6 -l kir+eng+rus' | ||||
|     text = pytesseract.image_to_string(gray, config=custom_config) | ||||
|         # Преобразование в оттенки серого | ||||
|         gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) | ||||
| 
 | ||||
|     # Получение координат и размеров квадратов | ||||
|     h, w, _ = image.shape | ||||
|     boxes = [(int(x), int(h - y_h), int(x_w - x), int(h - y)) for x, y, x_w, y_h in coordinates_list] | ||||
|         # Применение OCR | ||||
|         custom_config = r'--oem 3 --psm 6 -l kir+eng+rus' | ||||
|         text = pytesseract.image_to_string(gray, config=custom_config) | ||||
| 
 | ||||
|     # Фильтрация квадратов по проценту (если нужно) | ||||
|     percentage_threshold = 1 | ||||
|     filtered_boxes = [box for box in boxes if (box[2] * box[3]) / (w * h) * 100 >= percentage_threshold] | ||||
|         # Получение размеров изображения | ||||
|         h, w, _ = resized_image.shape | ||||
| 
 | ||||
|     # Извлечение текста из области каждого квадрата | ||||
|     texts = [] | ||||
|     for box in filtered_boxes: | ||||
|         x, y, x_w, y_h = box | ||||
|         crop_img = gray[y:y + y_h, x:x + x_w] | ||||
|         cropped_text = pytesseract.image_to_string(crop_img, config=custom_config) | ||||
|         texts.append(cropped_text) | ||||
|         print(f"Text inside the box at ({x}, {y}): {cropped_text}") | ||||
|         # Преобразование процентных координат в абсолютные | ||||
|         coordinates_list = [ | ||||
|             ( | ||||
|                 int(x * w / 100), | ||||
|                 int(y * h / 100), | ||||
|                 int(x_w * w / 100), | ||||
|                 int(y_h * h / 100) | ||||
|             ) | ||||
|             for x, y, x_w, y_h in percentage_coordinates_list | ||||
|         ] | ||||
| 
 | ||||
|     # Рисование квадратов и вывод текста | ||||
|     draw_boxes(image, filtered_boxes, texts) | ||||
|         # Фильтрация квадратов по проценту (если нужно) | ||||
|         percentage_threshold = 1 | ||||
|         filtered_boxes = [box for box in coordinates_list if (box[2] * box[3]) / (w * h) * 100 >= percentage_threshold] | ||||
| 
 | ||||
|     # Отображение изображения с квадратами с использованием matplotlib | ||||
|     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) | ||||
|     plt.title('Image with Boxes and Text') | ||||
|     plt.show() | ||||
|         # Извлечение текста из области каждого квадрата | ||||
|         texts = [] | ||||
|         for box in filtered_boxes: | ||||
|             x, y, x_w, y_h = box | ||||
|             crop_img = gray[y:y + y_h, x:x + x_w] | ||||
|             cropped_text = pytesseract.image_to_string(crop_img, config=custom_config) | ||||
|             texts.append(cropped_text) | ||||
|             print(f"Text inside the box at ({x}, {y}): {cropped_text}") | ||||
| 
 | ||||
|         # Рисование квадратов и вывод текста | ||||
|         draw_boxes(resized_image, filtered_boxes, texts) | ||||
| 
 | ||||
|         # Отображение изображения с квадратами с использованием matplotlib | ||||
|         plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)) | ||||
|         plt.title('Image with Boxes and Text') | ||||
|         plt.show() | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     image_path = 'img/img.png' | ||||
|         image_path = 'img/img.png' | ||||
| 
 | ||||
|     # Задайте координаты для каждого квадрата в формате (x, y, x_w, y_h) | ||||
|     coordinates_list = [(248, 399, 480, 330), (248, 399, 480, 282), (248, 399, 480, 230), (282, 399, 480, 190), (248, 399, 600, 160), (248, 399, 430, 120), (452, 399, 600, 103), (450, 395, 600, 60)] | ||||
|         # Задайте координаты для каждого квадрата в процентном соотношении (x, y, x_w, y_h) | ||||
|         percentage_coordinates_list = [(38, 23, 30, 11), (38, 35.5, 30, 10.5), (38, 48, 30, 7), (38, 56.4, 30, 5.5), | ||||
|                                        (38, 65, 45, 5.5), (38, 73, 20, 6), (68, 75.1, 20, 6.3), (68, 87, 20, 6)] | ||||
| 
 | ||||
|     process_image(image_path, coordinates_list) | ||||
|         process_image(image_path, percentage_coordinates_list) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user