未验证 提交 4784a3f2 编写于 作者: W wuzewu 提交者: GitHub

adapted nlpreader to lac module v2 (#418)

* adapted nlpreader to lac module v2

* fix code style of mask demo
上级 a1f8ea54
...@@ -8,6 +8,7 @@ import os ...@@ -8,6 +8,7 @@ import os
module = hub.Module(name="pyramidbox_lite_server_mask", version='1.1.0') module = hub.Module(name="pyramidbox_lite_server_mask", version='1.1.0')
# opencv输出中文 # opencv输出中文
def paint_chinese(im, chinese, position, fontsize, color_bgr): def paint_chinese(im, chinese, position, fontsize, color_bgr):
# 图像从OpenCV格式转换成PIL格式 # 图像从OpenCV格式转换成PIL格式
...@@ -88,7 +89,8 @@ while True: ...@@ -88,7 +89,8 @@ while True:
label_cn = "无口罩" label_cn = "无口罩"
cv2.rectangle(frame_copy, (left, top), (right, bottom), color, 3) cv2.rectangle(frame_copy, (left, top), (right, bottom), color, 3)
cv2.putText(frame_copy, label, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2) cv2.putText(frame_copy, label, (left, top - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
#origin_point = (left, top - 36) #origin_point = (left, top - 36)
#frame_copy = paint_chinese(frame_copy, label_cn, origin_point, 24, #frame_copy = paint_chinese(frame_copy, label_cn, origin_point, 24,
# color) # color)
......
...@@ -33,30 +33,27 @@ import argparse ...@@ -33,30 +33,27 @@ import argparse
def parse_args(): def parse_args():
parser = argparse.ArgumentParser('mask detection.') parser = argparse.ArgumentParser('mask detection.')
parser.add_argument('--models_dir', parser.add_argument(
type=str, '--models_dir', type=str, default='', help='path of models.')
default='', parser.add_argument(
help='path of models.') '--img_paths', type=str, default='', help='path of images')
parser.add_argument('--img_paths', parser.add_argument(
type=str, '--video_path', type=str, default='', help='path of video.')
default='', parser.add_argument(
help='path of images') '--use_camera',
parser.add_argument('--video_path', type=bool,
type=str, default=False,
default='', help='switch detect video or camera, default:video.')
help='path of video.') parser.add_argument(
parser.add_argument('--use_camera', '--open_imshow',
type=bool, type=bool,
default=False, default=False,
help='switch detect video or camera, default:video.') help='visualize video detection results in real time.')
parser.add_argument('--open_imshow', parser.add_argument(
type=bool, '--use_gpu',
default=False, type=bool,
help='visualize video detection results in real time.') default=False,
parser.add_argument('--use_gpu', help='switch cpu/gpu, default:cpu.')
type=bool,
default=False,
help='switch cpu/gpu, default:cpu.')
args = parser.parse_args() args = parser.parse_args()
return args return args
...@@ -108,10 +105,11 @@ class MaskClassifier: ...@@ -108,10 +105,11 @@ class MaskClassifier:
h, w = self.EVAL_SIZE[1], self.EVAL_SIZE[0] h, w = self.EVAL_SIZE[1], self.EVAL_SIZE[0]
inputs = [] inputs = []
for face in faces: for face in faces:
im = cv2.resize(face.rect_data, (128, 128), im = cv2.resize(
fx=0, face.rect_data, (128, 128),
fy=0, fx=0,
interpolation=cv2.INTER_CUBIC) fy=0,
interpolation=cv2.INTER_CUBIC)
# HWC -> CHW # HWC -> CHW
im = im.swapaxes(1, 2) im = im.swapaxes(1, 2)
im = im.swapaxes(0, 1) im = im.swapaxes(0, 1)
...@@ -151,10 +149,8 @@ class FaceDetector: ...@@ -151,10 +149,8 @@ class FaceDetector:
def Preprocess(self, image, shrink): def Preprocess(self, image, shrink):
h, w = int(image.shape[1] * shrink), int(image.shape[0] * shrink) h, w = int(image.shape[1] * shrink), int(image.shape[0] * shrink)
im = cv2.resize(image, (h, w), im = cv2.resize(
fx=0, image, (h, w), fx=0, fy=0, interpolation=cv2.INTER_CUBIC)
fy=0,
interpolation=cv2.INTER_CUBIC)
# HWC -> CHW # HWC -> CHW
im = im.swapaxes(1, 2) im = im.swapaxes(1, 2)
im = im.swapaxes(0, 1) im = im.swapaxes(0, 1)
...@@ -194,16 +190,18 @@ class FaceDetector: ...@@ -194,16 +190,18 @@ class FaceDetector:
def predict_images(args): def predict_images(args):
detector = FaceDetector(model_dir=args.models_dir + '/pyramidbox_lite/', detector = FaceDetector(
mean=[104.0, 177.0, 123.0], model_dir=args.models_dir + '/pyramidbox_lite/',
scale=[0.007843, 0.007843, 0.007843], mean=[104.0, 177.0, 123.0],
use_gpu=args.use_gpu, scale=[0.007843, 0.007843, 0.007843],
threshold=0.7) use_gpu=args.use_gpu,
threshold=0.7)
classifier = MaskClassifier(model_dir=args.models_dir + '/mask_detector/',
mean=[0.5, 0.5, 0.5], classifier = MaskClassifier(
scale=[1.0, 1.0, 1.0], model_dir=args.models_dir + '/mask_detector/',
use_gpu=args.use_gpu) mean=[0.5, 0.5, 0.5],
scale=[1.0, 1.0, 1.0],
use_gpu=args.use_gpu)
names = [] names = []
image_paths = [] image_paths = []
for name in os.listdir(args.img_paths): for name in os.listdir(args.img_paths):
...@@ -229,16 +227,18 @@ def predict_video(args, im_shape=(1920, 1080), use_camera=False): ...@@ -229,16 +227,18 @@ def predict_video(args, im_shape=(1920, 1080), use_camera=False):
capture = cv2.VideoCapture(0) capture = cv2.VideoCapture(0)
else: else:
capture = cv2.VideoCapture(args.video_path) capture = cv2.VideoCapture(args.video_path)
detector = FaceDetector(model_dir=args.models_dir + '/pyramidbox_lite/', detector = FaceDetector(
mean=[104.0, 177.0, 123.0], model_dir=args.models_dir + '/pyramidbox_lite/',
scale=[0.007843, 0.007843, 0.007843], mean=[104.0, 177.0, 123.0],
use_gpu=args.use_gpu, scale=[0.007843, 0.007843, 0.007843],
threshold=0.7) use_gpu=args.use_gpu,
threshold=0.7)
classifier = MaskClassifier(model_dir=args.models_dir + '/mask_detector/',
mean=[0.5, 0.5, 0.5], classifier = MaskClassifier(
scale=[1.0, 1.0, 1.0], model_dir=args.models_dir + '/mask_detector/',
use_gpu=args.use_gpu) mean=[0.5, 0.5, 0.5],
scale=[1.0, 1.0, 1.0],
use_gpu=args.use_gpu)
path = './result' path = './result'
isExists = os.path.exists(path) isExists = os.path.exists(path)
...@@ -248,8 +248,8 @@ def predict_video(args, im_shape=(1920, 1080), use_camera=False): ...@@ -248,8 +248,8 @@ def predict_video(args, im_shape=(1920, 1080), use_camera=False):
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v') fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(os.path.join(path, 'result.mp4'), fourcc, fps, writer = cv2.VideoWriter(
(width, height)) os.path.join(path, 'result.mp4'), fourcc, fps, (width, height))
import time import time
start_time = time.time() start_time = time.time()
index = 0 index = 0
......
...@@ -1162,9 +1162,6 @@ class LACClassifyReader(BaseReader): ...@@ -1162,9 +1162,6 @@ class LACClassifyReader(BaseReader):
self.tokenizer = tokenization.FullTokenizer( self.tokenizer = tokenization.FullTokenizer(
vocab_file=vocab_path, do_lower_case=False) vocab_file=vocab_path, do_lower_case=False)
self.vocab = self.tokenizer.vocab self.vocab = self.tokenizer.vocab
self.feed_key = list(
self.lac.processor.data_format(
sign_name="lexical_analysis").keys())[0]
self.has_processed = { self.has_processed = {
"train": False, "train": False,
"dev": False, "dev": False,
...@@ -1200,7 +1197,7 @@ class LACClassifyReader(BaseReader): ...@@ -1200,7 +1197,7 @@ class LACClassifyReader(BaseReader):
"Unknown phase, which should be in ['train', 'dev', 'test'].") "Unknown phase, which should be in ['train', 'dev', 'test'].")
def preprocess(text): def preprocess(text):
data_dict = {self.feed_key: [text]} data_dict = {'text': [text]}
processed = self.lac.lexical_analysis(data=data_dict) processed = self.lac.lexical_analysis(data=data_dict)
processed = [ processed = [
self.vocab[word] for word in processed[0]['word'] self.vocab[word] for word in processed[0]['word']
......
...@@ -13,5 +13,5 @@ ...@@ -13,5 +13,5 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
""" PaddleHub version string """ """ PaddleHub version string """
hub_version = "1.5.2" hub_version = "1.6.0"
module_proto_version = "1.0.0" module_proto_version = "1.0.0"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册