File size: 3,271 Bytes
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
import os
import shutil
import subprocess
from PIL import Image
import directories as Dir

def clearDir():

    #/text_detection/cookie/user_input
    #shutil.rmtree('/cookie')
    #os.remove("/cookie/user_input.jpg")

    #cropped_img_path = "/runs/detect/" + cropped_img_folder_name
    shutil.rmtree(Dir.cropped_img_path) #'/runs/detect/user_output'

    #txt_file_path = "/HCR/TextRecognition/log_demo_result.txt"  
    os.remove(Dir.txt_file_path)

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