main.py
1    import numpy as np
2    import cv2
3    import math
4    from scipy import ndimage
5    import pytesseract
6    import os
7    
8    directory = '/Users/ole/Desktop/Derma.folder/'
9    
10   # iterate over files in
11   # that directory
12   count = 1
13   for filename in os.listdir(directory):
14       f = os.path.join(directory, filename)
15       # checking if it is a file
16       if os.path.isfile(f):
17           print(f)
18           try:
19   
20               IMAGE_FILE_LOCATION = f
21               input_img = cv2.imread(IMAGE_FILE_LOCATION)
22   
23               coordinates = []
24   
25   
26               # Defining the event listener (callback function)
27               def shape_selection(event, x, y, flags, param):
28                   # making coordinates global
29                   global coordinates
30   
31                   # Storing the (x1,y1) coordinates when left mouse button is pressed
32                   if event == cv2.EVENT_LBUTTONDOWN:
33                       coordinates = [(x, y)]
34   
35                       # Storing the (x2,y2) coordinates when the left mouse button is released and make a rectangle on the selected region
36                   elif event == cv2.EVENT_LBUTTONUP:
37                       coordinates.append((x, y))
38   
39                       # Drawing a rectangle around the region of interest (roi)
40                       cv2.rectangle(image, coordinates[0], coordinates[1], (0, 0, 255), 2)
41                       cv2.imshow("image", image)
42   
43                   # load the image, clone it, and setup the mouse callback function
44   
45               image = input_img
46               image_copy = image.copy()
47               coordinates = []
48               coordinates = [(1400, 1900)]
49               coordinates.append((1400, 1900))
50   
51               image_roi = image_copy[coordinates[0][1]:coordinates[1][1], coordinates[0][0]:coordinates[1][0]]
52               # TO SEE THE CROPPED IMAGE
53               while True:
54                   # display the image and wait for a keypress
55                   cv2.imshow("image", image_roi)
56                   key = cv2.waitKey(1) & 0xFF
57   
58                   if key == 13:  # If 'enter' is pressed, apply OCR
59                       break
60   
61               text = pytesseract.image_to_string(image_roi,config='--psm 6')
62               # print("The text in the selected region is as follows:")
63               print(text)
64               os.rename(f, f"{text} {count}.jpg")
65               count = count + 1
66   
67           except:
68               pass