From 2d55c3a5f5d17b7c5e9d86ac5f7b7a96c84f7eae Mon Sep 17 00:00:00 2001
From: merschie <f-mersch@web.de>
Date: Thu, 26 Oct 2023 13:54:00 +0200
Subject: [PATCH] get Namelist from client

---
 src/preprocessor.py |   2 +-
 src/webserver.py    | 152 ++++++++++++--------------------------------
 2 files changed, 42 insertions(+), 112 deletions(-)

diff --git a/src/preprocessor.py b/src/preprocessor.py
index ea634b8..7b21d81 100644
--- a/src/preprocessor.py
+++ b/src/preprocessor.py
@@ -165,7 +165,7 @@ class Preprocessor:
                 #cut out first horizontal line if it is white
                 img = 255-img
                 try:
-                    while np.sum(img[0,:]) < 2048:
+                    while np.sum(img[0,:]) < 1024:
                         img = img[1:,:]
                 except:
                     return np.ones((ht, ht))
diff --git a/src/webserver.py b/src/webserver.py
index 7bd016c..e1c19e4 100644
--- a/src/webserver.py
+++ b/src/webserver.py
@@ -1,67 +1,66 @@
 from flask import Flask, request, jsonify
-
 import main as htr
 import model as htr_model
 import dataloader_iam as htr_data_loader
 import preprocessor as htr_preprocessor
 import numpy as np
 import base64
-import time
+import json
 
 app = Flask(__name__)
 
-image_size = 32
-
 model_name = htr_model.Model(htr.char_list_from_file(), htr_model.DecoderType.LexiconSearch, must_restore=True)
 model_name.setup_ctc
-csv_path = '../tns.csv'
 
 char_list = htr.char_list_from_file()
 chars = ''.join(char_list)
-#word_chars = open('../model/wordCharList.txt').read().splitlines()[0]
-matrikel_numbers = []
-
-@app.route('/getNames', methods=['GET'])
-def getNames():
-    return jsonify(matrikel_numbers)
-
 
 
+def image_to_base64(processed_image):
+    processed_image = processed_image + 0.5
+    processed_image = processed_image * 255
+    #rotate image 90 degrees
+    processed_image = np.rot90(processed_image,3)
+    #mirror image
+    image = np.fliplr(processed_image)
+    height, width = image.shape
+    cut = np.reshape(image,(height*width))
+    cut = np.append(cut,height)
+    cut = np.append(cut,width)
+    cut = cut.astype(np.uint64)
+    array_bytes = cut.tobytes()
+    image_base64 = base64.b64encode(array_bytes).decode('utf-8')
+    return image_base64
 
-@app.route('/predictNachname', methods=['POST'])
-def predictNach():
-    image_array = np.frombuffer(request.data, dtype=np.uint64)
+def base64_to_image(processed_image):
+    image=base64.b64decode(processed_image.encode('utf-8'))
+    image_array = np.frombuffer(image, dtype=np.uint64)
     h=image_array[-2]
     w=image_array[-1]
     image_array = image_array[:-2]
     image_array = image_array.reshape((h, w))
+    return image_array
+
+
+
+@app.route('/predictNachname', methods=['POST'])
+def predictNach():
+    image_data=json.loads(request.data)['image']
+    
+    image_array=base64_to_image(image_data)
+    
     preprocessor = htr_preprocessor.Preprocessor(htr.get_img_size(), dynamic_width=True, padding=16)
     processed_image = preprocessor.process_img(image_array)
     batch = htr_data_loader.Batch([processed_image], None, 1)
 
     #change corpus for name
-    model_name.corpus = open('../data/Nachname.txt').read().split()[:200]
+    names=json.loads(request.data)['names']
+    print(names)
+    model_name.corpus = names.split(' ')
 
-    #check time of infer_batch
     recognized, probability = htr_model.Model.infer_batch(model_name, batch)
 
-
-    processed_image = processed_image + 0.5
-    processed_image = processed_image * 255
-    #rotate image 90 degrees
-    processed_image = np.rot90(processed_image,3)
-    #mirror image
-    processed_image = np.fliplr(processed_image)
-    height, width = processed_image.shape
-    image = np.reshape(processed_image,(height*width))
-    image = np.append(image,height)
-    image = np.append(image,width)
-    image = image.astype(np.uint64)
-    array_bytes = image.tobytes()
-    image_base64 = base64.b64encode(array_bytes).decode('utf-8')
-
-
-
+    image_base64 = image_to_base64(processed_image)
 
     result = {
         'recognized': recognized,
@@ -73,36 +72,21 @@ def predictNach():
 
 @app.route('/predictVorname', methods=['POST'])
 def predictVor():
-    image_array = np.frombuffer(request.data, dtype=np.uint64)
-    h=image_array[-2]
-    w=image_array[-1]
-    image_array = image_array[:-2]
-    image_array = image_array.reshape((h, w))
+    image_data=json.loads(request.data)['image']
+    
+    image_array=base64_to_image(image_data)
+    
     preprocessor = htr_preprocessor.Preprocessor(htr.get_img_size(), dynamic_width=True, padding=16)
     processed_image = preprocessor.process_img(image_array)
     batch = htr_data_loader.Batch([processed_image], None, 1)
 
-
     #change corpus for name
-    model_name.corpus = open('../data/Vorname.txt').read().split()[:200]
-    recognized, probability = htr_model.Model.infer_batch(model_name, batch)
+    names=json.loads(request.data)['names']
+    model_name.corpus = names.split(' ')
 
+    recognized, probability = htr_model.Model.infer_batch(model_name, batch)
 
-
-    processed_image = processed_image + 0.5
-    processed_image = processed_image * 255
-    #rotate image -90 degrees
-    processed_image = np.rot90(processed_image,3)
-    #mirror image
-    processed_image = np.fliplr(processed_image)
-    height, width = processed_image.shape
-    image = np.reshape(processed_image,(height*width))
-    image = np.append(image,height)
-    image = np.append(image,width)
-    image = image.astype(np.uint64)
-    array_bytes = image.tobytes()
-    image_base64 = base64.b64encode(array_bytes).decode('utf-8')
-
+    image_base64 = image_to_base64(processed_image)
 
     result = {
         'recognized': recognized,
@@ -110,60 +94,6 @@ def predictVor():
     }
     return jsonify(result)
 
-def replace_umlauts(text):
-    text = text.replace('ä', 'a')
-    text = text.replace('ö', 'o')
-    text = text.replace('ü', 'u')
-    text = text.replace('Ä', 'A')
-    text = text.replace('Ö', 'O')
-    text = text.replace('Ü', 'U')
-    text = text.replace('ß', 's')
-    text = text.replace('é', 'e')
-    text = text.replace('è', 'e')
-    text = text.replace('ê', 'e')
-    text = text.replace('à', 'a')
-    text = text.replace('â', 'a')
-    text = text.replace('á', 'a')
-    text = text.replace('ô', 'o')
-    text = text.replace('û', 'u')
-    text = text.replace('ç', 'c')
-    text = text.replace('î', 'i')
-    text = text.replace('ï', 'i')
-    text = text.replace('ë', 'e')
-    text = text.replace('ù', 'u')
-    text = text.replace(' ', '-')
-    return text
-
-
-def split_Student_Names():
-    #csv looks like: Vorname;Nachname;Matrikelnummer
-    #need to put Vorname in one list, Nachname in another
-
-    #create /data/Vorname.txt and /data/Nachname.txt
-    vorname_file = open('../data/Vorname.txt', 'w')
-    nachname_file = open('../data/Nachname.txt', 'w')
-    matrikelnummer_file = open('../data/Matrikelnummer.txt', 'w')
-    teilnehmer=[]
-    with open(csv_path, 'r') as csv_file:
-        lines = csv_file.readlines()
-        for line in lines[1:]:
-            line = line.split(',')
-            vorname = line[2][:-1]
-            vorname = replace_umlauts(vorname)
-            nachname = line[1]
-            nachname = replace_umlauts(nachname)
-            matrikelnummer = line[0]
-            print(vorname, nachname, matrikelnummer)
-            vorname_file.write(vorname + " ")
-            nachname_file.write(nachname + " ")
-            matrikelnummer_file.write(matrikelnummer + " ")
-            teilnehmer.append([vorname,nachname,matrikelnummer])
-    return teilnehmer
-
-
 
 if __name__ == '__main__':
-    #split csv file into Vorname, Nachname and Matrikelnummer
-    matrikel_numbers = split_Student_Names()
-
     app.run(debug=False,port=8000)
-- 
GitLab