File size: 4,281 Bytes
30c8b41
 
 
 
 
 
 
 
1b00da7
 
 
 
 
 
 
 
 
 
 
 
 
 
8cd31d2
b00f0f5
e285fc9
 
30c8b41
 
1b00da7
 
8cd31d2
b00f0f5
e285fc9
 
1b00da7
 
8cd31d2
 
 
b00f0f5
e285fc9
 
30c8b41
b00f0f5
 
30c8b41
 
 
 
1fcea5a
 
 
30c8b41
 
 
 
 
1fcea5a
c313555
30c8b41
 
1fcea5a
30c8b41
 
d9bba41
30c8b41
6de4b7f
30c8b41
d321856
30c8b41
 
 
 
 
 
 
 
 
 
 
 
 
 
1fcea5a
0e365d4
30c8b41
 
 
1fcea5a
30c8b41
e0be3d4
30c8b41
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import shutil
import subprocess
from PIL import Image
import directories as Dir

def clearDir():

    #์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ๋“ค์„ ์‚ญ์ œํ•จ์œผ๋กœ์จ ์ค‘๋ณต ์ถœ๋ ฅ ๋ฐฉ์ง€
    # 1. User input
    #   "/TextDetection/cookie/user_input.jpg" ์‚ญ์ œ
    # 2. Cropped images
    #   TextDetection/runs/detect/ ํ•˜์œ„ ํŒŒ์ผ ์ „๋ถ€ ์‚ญ์ œ
    # 3. Recognition Result
    #   /TextRecognition/log_demo_result.txt ์‚ญ์ œ

    user_input = "/home/user/app"+ Dir.yolo_dir +"/cookie/user_input.jpg"
    crops = "/home/user/app" + Dir.yolo_dir + Dir.cropped_img_path
    result = "/home/user/app" + Dir.txt_file_path

    if os.path.isfile(user_input):
        os.remove(user_input)
        print(user_input)
        print('Message: user input clear - {}', os.path.isfile(user_input))
    else:
        print('Message: no user input')

    #cropped_img_path = "/runs/detect/" + cropped_img_folder_name
    if os.path.isdir(crops):
        shutil.rmtree(crops) #'/runs/detect/user_output'
        print(crops)
        print('Message: crop folder clear - {}', os.path.isdir(crops))
    else:
        print('Message: no crop folder')
    
    #txt_file_path = "/log_demo_result.txt"  
    if os.path.isfile("log_demo_result.txt"):
        os.remove("log_demo_result.txt")
        print("log_demo_result.txt")
        print('Message: result clear - {}', os.path.isfile("log_demo_result.txt"))
    else:
        print('Message: no result')

    return print("All Cleared")

def textDetection(im):

    #change dir to yolo folder
    #yolo_dir = "/HCR/TextDetection/"
    #subprocess.call('pwd', shell=True)
    #subprocess.call('ls', shell=True)
    ## subprocess.call('sudo cd '+ Dir.yolo_dir, shell=True)

    #transfrom ndarray type to PIL type
    im = Image.fromarray(im)

    # save input image to cookie folder
    ## subprocess.call('cd cookie', shell=True)
    im.save("/home/user/app"+Dir.yolo_dir+"/cookie/user_input.jpg", 'JPEG')

    #yolo_dir = "/HCR/TextDetection/"
    ## subprocess.call('cd '+ Dir.yolo_dir, shell=True)

    # (Shell) run detect.py to get cropped word images
    subprocess.call(['python', "/home/user/app"+Dir.yolo_dir+'/detect.py',
                  #User Input Data : /text_detection/cookie
                 '--source',"/home/user/app"+Dir.yolo_dir+'/cookie',
                  #Text Detection Model : /runs/wordDetection/weights/best.pt
                 '--weights', "/home/user/app"+Dir.detect_model_dir,
                 '--conf','0.25',
                  #Output Images Save Directory /runs/detect/user_output
                 '--name', Dir.cropped_img_folder_name,
                 '--save-crop',
                 '--save-conf'])

    #g = (size / max(im.size))  # gain
    #im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS)  # resize

    #results = model(im)  # inference
    #results.render()  # updates results.imgs with boxes and labels
    #return Image.fromarray(results.imgs[0])

def textRearrange():
    ## subprocess.call('cd ' + Dir.DBSCAN_dir, shell=True)
    subprocess.call(['python', "/home/user/app"+Dir.DBSCAN_dir+'/DBSCAN.py'])

def textRecognition():
    #%cd /content/drive/MyDrive/KITA/Text/lmdb/deep-text-recognition-benchmark
    ## subprocess.call('cd '+Dir.recog_dir, shell=True)
    #!CUDA_VISIBLE_DEVICES=0 python3 demo.py --Transformation TPS --FeatureExtraction ResNet --SequenceModeling BiLSTM --Prediction Attn --image_folder /content/drive/MyDrive/KITA/Text/YOLO/runs/detect/youtube_data2/crops/word --saved_model /content/drive/MyDrive/KITA/Text/best_accuracy_s/best_accuracy_s.pth
    subprocess.call('CUDA_VISIBLE_DEVICES="" python3 '+ "/home/user/app"+ Dir.recog_dir +'/demo.py --Transformation TPS --FeatureExtraction ResNet --SequenceModeling BiLSTM --Prediction Attn --image_folder /home/user/app' + Dir.yolo_dir + Dir.cropped_img_path + '/crops/word --saved_model /home/user/app'+Dir.recog_model_dir, shell=True)

def getHcrResult(file_path):#*#
    texts = ""
    with open(file_path, 'r') as file:
        lines = file.readlines()
    for line in lines[3:]:
      line = line.replace("\t","*",1)
      line = line.replace(" ","*",1)
      parts = line.replace(" ","")
      parts2 = parts.split("*",2)
      #print(len(parts2))
      texts = texts +" "+ str(parts2[1:2])[2:-2]

    return texts