Prechádzať zdrojové kódy

set encoding when open files in windows

FlyingQianMM 4 rokov pred
rodič
commit
194420c20c

+ 3 - 2
paddlex_restful/restful/app.py

@@ -24,6 +24,7 @@ import sys
 import multiprocessing as mp
 from . import workspace_pb2 as w
 from .utils import CustomEncoder, ShareData, is_pic, get_logger, TaskStatus, get_ip
+from paddlex.utils import get_encoding
 import numpy as np
 
 app = Flask(__name__)
@@ -929,14 +930,14 @@ def gui():
             osp.dirname(__file__), 'templates', 'paddlex_restful_demo.html')
         ip = get_ip()
         url = 'var str_srv_url = "http://' + ip + ':' + str(SD.port) + '";'
-        f = open(file_path, 'r+')
+        f = open(file_path, 'r+', encoding=get_encoding(file_path))
         lines = f.readlines()
         for i, line in enumerate(lines):
             if '0.0.0.0:8080' in line:
                 lines[i] = url
                 break
         f.close()
-        f = open(file_path, 'w+')
+        f = open(file_path, 'w+', encoding=get_encoding(file_path))
         f.writelines(lines)
         f.close()
         return render_template('/paddlex_restful_demo.html')

+ 2 - 1
paddlex_restful/restful/dataset/utils.py

@@ -21,6 +21,7 @@ from PIL import Image
 import numpy as np
 import json
 from ..utils import set_folder_status, DatasetStatus
+from paddlex.utils import get_encoding
 
 
 def list_files(dirname):
@@ -145,7 +146,7 @@ def check_list_txt(list_txts):
     for list_txt in list_txts:
         if not osp.exists(list_txt):
             continue
-        with open(list_txt) as f:
+        with open(list_txt, encoding=get_encoding(list_txt)) as f:
             for line in f:
                 items = line.strip().split()
                 if len(items) != 2:

+ 7 - 2
paddlex_restful/restful/project/evaluate/classification.py

@@ -17,13 +17,18 @@ import yaml
 import os.path as osp
 import numpy as np
 from sklearn.metrics import confusion_matrix, roc_curve, auc
+from paddlex.utils import get_encoding
 
 
 class Evaluator(object):
     def __init__(self, model_path, topk=5):
-        with open(osp.join(model_path, "model.yml")) as f:
+        model_yml = osp.join(model_path, "model.yml")
+        with open(model_yml, encoding=get_encoding(model_yml)) as f:
             model_info = yaml.load(f.read(), Loader=yaml.Loader)
-        with open(osp.join(model_path, 'eval_details.json'), 'r') as f:
+        eval_details_file = osp.join(model_path, 'eval_details.json')
+        with open(
+                eval_details_file, 'r',
+                encoding=get_encoding(eval_details_file)) as f:
             eval_details = json.load(f)
         self.topk = topk
 

+ 6 - 1
paddlex_restful/restful/project/evaluate/detection.py

@@ -18,6 +18,8 @@ import copy
 import os.path as osp
 import numpy as np
 
+from paddlex.utils import get_encoding
+
 backup_linspace = np.linspace
 
 
@@ -409,7 +411,10 @@ class DetEvaluator(object):
         self.score_threshold = score_threshold if score_threshold is not None else .3
 
     def _prepare_data(self):
-        with open(osp.join(self.model_path, 'eval_details.json'), 'r') as f:
+        eval_details_file = osp.join(self.model_path, 'eval_details.json')
+        with open(
+                eval_details_file, 'r',
+                encoding=get_encoding(eval_details_file)) as f:
             eval_details = json.load(f)
         self.bbox = eval_details['bbox']
         self.mask = None

+ 7 - 2
paddlex_restful/restful/project/evaluate/segmentation.py

@@ -16,14 +16,19 @@ import json
 import yaml
 import os.path as osp
 import numpy as np
+from paddlex.utils import get_encoding
 
 
 class Evaluator(object):
     def __init__(self, model_path):
-        with open(osp.join(model_path, "model.yml")) as f:
+        model_yml = osp.join(model_path, "model.yml")
+        with open(model_yml, encoding=get_encoding(model_yml)) as f:
             model_info = yaml.load(f.read(), Loader=yaml.Loader)
         self.labels = model_info['_Attributes']['labels']
-        with open(osp.join(model_path, 'eval_details.json'), 'r') as f:
+        eval_details_file = osp.join(model_path, 'eval_details.json')
+        with open(
+                eval_details_file, 'r',
+                encoding=get_encoding(eval_details_file)) as f:
             eval_details = json.load(f)
         self.confusion_matrix = np.array(eval_details['confusion_matrix'])
         self.num_classes = len(self.confusion_matrix)

+ 9 - 2
paddlex_restful/restful/project/operate.py

@@ -27,6 +27,7 @@ from ..utils import (pkill, set_folder_status, get_folder_status, TaskStatus,
                      PredictStatus, PruneStatus)
 from .evaluate.draw_pred_result import visualize_classified_result, visualize_detected_result, visualize_segmented_result
 from .visualize import plot_det_label, plot_insseg_label, get_color_map_list
+from paddlex.utils import get_encoding
 
 
 def _call_paddle_prune(best_model_path, prune_analysis_path, params):
@@ -198,7 +199,10 @@ def _call_paddlex_predict(task_path,
                     osp.join(dataset_path, "test_list.txt")):
                 if len(img_list) == 0 and osp.exists(
                         osp.join(dataset_path, "test_list.txt")):
-                    with open(osp.join(dataset_path, "test_list.txt")) as f:
+                    with open(
+                            osp.join(dataset_path, "test_list.txt"),
+                            encoding=get_encoding(
+                                osp.join(dataset_path, "test_list.txt"))) as f:
                         for line in f:
                             items = line.strip().split()
                             file_list[osp.join(dataset_path, items[0])] = \
@@ -271,7 +275,10 @@ def _call_paddlex_predict(task_path,
         if img_data is None:
             if len(img_list) == 0 and osp.exists(
                     osp.join(dataset_path, "test_list.txt")):
-                with open(osp.join(dataset_path, "test_list.txt")) as f:
+                with open(
+                        osp.join(dataset_path, "test_list.txt"),
+                        encoding=get_encoding(
+                            osp.join(dataset_path, "test_list.txt"))) as f:
                     for line in f:
                         items = line.strip().split()
                         file_list[osp.join(dataset_path, items[0])] = \

+ 2 - 1
paddlex_restful/restful/project/task.py

@@ -24,6 +24,7 @@ import xlwt
 import numpy as np
 from ..utils import set_folder_status, TaskStatus, get_folder_status, is_available, get_ip, trans_name
 from .train.params import ClsParams, DetParams, SegParams
+from paddlex.utils import get_encoding
 
 
 def create_task(data, workspace):
@@ -742,7 +743,7 @@ def get_quant_result(data, workspace):
     result = {}
     import json
     if osp.exists(result_json):
-        with open(result_json, 'r') as f:
+        with open(result_json, 'r', encoding=get_encoding(result_json)) as f:
             result = json.load(f)
     return {'status': 1, 'quant_result': result}
 

+ 6 - 2
paddlex_restful/restful/workspace.py

@@ -24,6 +24,7 @@ import configparser
 import time
 import shutil
 import copy
+from paddlex.utils import get_encoding
 
 
 class Workspace():
@@ -256,8 +257,11 @@ class Workspace():
         #更新程序配置信息
         app_conf_file_name = 'PaddleX'.lower() + ".cfg"
 
-        with open(os.path.join(PADDLEX_HOME, app_conf_file_name),
-                  'w+') as file:
+        with open(
+                os.path.join(PADDLEX_HOME, app_conf_file_name),
+                'w+',
+                encoding=get_encoding(
+                    os.path.join(PADDLEX_HOME, app_conf_file_name))) as file:
             self.m_cfgfile.write(file)