Abhishek Gola commited on
Commit
8a52c41
·
1 Parent(s): cf70a14

Added pphumanseg model to space

Browse files
Files changed (4) hide show
  1. README.md +6 -0
  2. app.py +67 -0
  3. pphumanseg.py +69 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -7,6 +7,12 @@ sdk: gradio
7
  sdk_version: 5.34.1
8
  app_file: app.py
9
  pinned: false
 
 
 
 
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 5.34.1
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Human segmentation using OpenCV PaddlePaddle humanseg ONNX model with Gradio UI
11
+ tags:
12
+ - opencv
13
+ - Human segmentation
14
+ - PaddlePaddle
15
+ - Humanseg
16
  ---
17
 
18
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+ import numpy as np
3
+ import gradio as gr
4
+ from huggingface_hub import hf_hub_download
5
+ from pphumanseg import PPHumanSeg
6
+
7
+ # Download ONNX model from Hugging Face
8
+ model_path = hf_hub_download(repo_id="opencv/human_segmentation_pphumanseg", filename="human_segmentation_pphumanseg_2023mar.onnx")
9
+
10
+ # Initialize PPHumanSeg model
11
+ model = PPHumanSeg(
12
+ modelPath=model_path,
13
+ backendId=cv.dnn.DNN_BACKEND_OPENCV,
14
+ targetId=cv.dnn.DNN_TARGET_CPU
15
+ )
16
+
17
+ def get_color_map_list(num_classes):
18
+ num_classes += 1
19
+ color_map = num_classes * [0, 0, 0]
20
+ for i in range(num_classes):
21
+ j = 0
22
+ lab = i
23
+ while lab:
24
+ color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
25
+ color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
26
+ color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
27
+ j += 1
28
+ lab >>= 3
29
+ return color_map[3:]
30
+
31
+ def visualize(image, result, weight=0.6):
32
+ color_map = get_color_map_list(256)
33
+ color_map = np.array(color_map).reshape(256, 3).astype(np.uint8)
34
+
35
+ c1 = cv.LUT(result, color_map[:, 0])
36
+ c2 = cv.LUT(result, color_map[:, 1])
37
+ c3 = cv.LUT(result, color_map[:, 2])
38
+ pseudo_img = np.dstack((c1, c2, c3))
39
+
40
+ vis_result = cv.addWeighted(image, weight, pseudo_img, 1 - weight, 0)
41
+ return vis_result
42
+
43
+ def segment_person(input_image):
44
+ image = cv.cvtColor(input_image, cv.COLOR_RGB2BGR)
45
+ h, w, _ = image.shape
46
+ resized = cv.resize(image, (192, 192))
47
+ resized = cv.cvtColor(resized, cv.COLOR_BGR2RGB)
48
+
49
+ result = model.infer(resized)
50
+ result = cv.resize(result[0, :, :], dsize=(w, h), interpolation=cv.INTER_NEAREST)
51
+
52
+ output = visualize(image, result)
53
+ output = cv.cvtColor(output, cv.COLOR_BGR2RGB)
54
+ return output
55
+
56
+ # Gradio Interface
57
+ demo = gr.Interface(
58
+ fn=segment_person,
59
+ inputs=gr.Image(type="numpy", label="Upload Image"),
60
+ outputs=gr.Image(type="numpy", label="Human Segmentation Output"),
61
+ title="Human Segmentation PPHumanSeg (OpenCV DNN)",
62
+ allow_flagging="never",
63
+ description="Upload an image to segment human regions using OpenCV's ONNX-based PPHumanSeg model."
64
+ )
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch()
pphumanseg.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is part of OpenCV Zoo project.
2
+ # It is subject to the license terms in the LICENSE file found in the same directory.
3
+ #
4
+ # Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5
+ # Third party copyrights are property of their respective owners.
6
+
7
+ import numpy as np
8
+ import cv2 as cv
9
+
10
+ class PPHumanSeg:
11
+ def __init__(self, modelPath, backendId=0, targetId=0):
12
+ self._modelPath = modelPath
13
+ self._backendId = backendId
14
+ self._targetId = targetId
15
+
16
+ self._model = cv.dnn.readNet(self._modelPath)
17
+ self._model.setPreferableBackend(self._backendId)
18
+ self._model.setPreferableTarget(self._targetId)
19
+
20
+ self._inputNames = ''
21
+ self._outputNames = ['save_infer_model/scale_0.tmp_1']
22
+ self._currentInputSize = None
23
+ self._inputSize = [192, 192]
24
+ self._mean = np.array([0.5, 0.5, 0.5])[np.newaxis, np.newaxis, :]
25
+ self._std = np.array([0.5, 0.5, 0.5])[np.newaxis, np.newaxis, :]
26
+
27
+ @property
28
+ def name(self):
29
+ return self.__class__.__name__
30
+
31
+ def setBackendAndTarget(self, backendId, targetId):
32
+ self._backendId = backendId
33
+ self._targetId = targetId
34
+ self._model.setPreferableBackend(self._backendId)
35
+ self._model.setPreferableTarget(self._targetId)
36
+
37
+ def _preprocess(self, image):
38
+
39
+ image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
40
+
41
+ self._currentInputSize = image.shape
42
+ image = cv.resize(image, (192, 192))
43
+
44
+ image = image.astype(np.float32, copy=False) / 255.0
45
+ image -= self._mean
46
+ image /= self._std
47
+ return cv.dnn.blobFromImage(image)
48
+
49
+ def infer(self, image):
50
+
51
+ # Preprocess
52
+ inputBlob = self._preprocess(image)
53
+
54
+ # Forward
55
+ self._model.setInput(inputBlob, self._inputNames)
56
+ outputBlob = self._model.forward()
57
+
58
+ # Postprocess
59
+ results = self._postprocess(outputBlob)
60
+
61
+ return results
62
+
63
+ def _postprocess(self, outputBlob):
64
+
65
+ outputBlob = outputBlob[0]
66
+ outputBlob = cv.resize(outputBlob.transpose(1,2,0), (self._currentInputSize[1], self._currentInputSize[0]), interpolation=cv.INTER_LINEAR).transpose(2,0,1)[np.newaxis, ...]
67
+
68
+ result = np.argmax(outputBlob, axis=1).astype(np.uint8)
69
+ return result
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv-python
2
+ gradio
3
+ numpy
4
+ huggingface_hub