diff --git a/.gitignore b/.gitignore index 1cf00db..1e61422 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ nlp/__pycache__/ __pycache__/ pyWeb/ research/imgcensor/model/ -cv/model/ \ No newline at end of file +cv/model/ +cv/static/ImgCensorUpload/ +cv/static/SkinUpload/ diff --git a/cv/controllers.py b/cv/controllers.py index 6e81211..42baf60 100644 --- a/cv/controllers.py +++ b/cv/controllers.py @@ -206,8 +206,73 @@ def infer_from_img(self, img): } +class NSFWEstimator: + """ + NSFW Estimator Class Wrapper + """ + + def __init__(self, pretrained_model_path="cv/model/DenseNet121_NSFW.pth", num_cls=5): + self.num_cls = num_cls + model = models.densenet121(pretrained=True) + num_ftrs = model.classifier.in_features + model.classifier = nn.Linear(num_ftrs, self.num_cls) + + device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') + + model.load_state_dict(torch.load(pretrained_model_path)) + + model.to(device) + model.eval() + + self.device = device + self.model = model + self.topK = 5 + self.mapping = { + 0: 'drawings', + 1: 'hentai', + 2: 'neutral', + 3: 'porn', + 4: 'sexy' + } + + def infer(self, img_file): + img = Image.open(img_file) + + preprocess = transforms.Compose([ + transforms.Resize(224), + transforms.CenterCrop(224), + transforms.ToTensor(), + transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) + ]) + + img = preprocess(img) + img.unsqueeze_(0) + + img = img.to(self.device) + + outputs = self.model(img) + outputs = F.softmax(outputs, dim=1) + + topK_prob, topK_label = torch.topk(outputs, self.topK) + prob = topK_prob.to("cpu").detach().numpy().tolist() + + _, predicted = torch.max(outputs.data, 1) + + return { + "status": 0, + "message": "success", + "results": [ + { + "prob": round(prob[0][i], 4), + "type": self.mapping[int(topK_label[0][i].to("cpu"))], + } for i in range(self.topK) + ] + } + + beauty_recognizer = BeautyRecognizer() skin_disease_recognizer = SkinDiseaseRecognizer(num_cls=198) +nsfw_estimator = NSFWEstimator(num_cls=5) def upload_and_rec_beauty(request): @@ -334,3 +399,55 @@ def upload_and_rec_skin_disease(request): json_result = json.dumps(result, ensure_ascii=False) return HttpResponse(json_result) + + +def upload_and_rec_porn(request): + """ + upload and recognize porn image + :param request: + :return: + """ + image_dir = 'cv/static/ImgCensorUpload' + if not os.path.exists(image_dir): + os.makedirs(image_dir) + + result = {} + + if request.method == "POST": + image = request.FILES.get("image", None) + if not image: + result['code'] = 3 + result['msg'] = 'Invalid Path for Image' + result['results'] = None + + json_result = json.dumps(result, ensure_ascii=False) + + return HttpResponse(json_result) + else: + destination = open(os.path.join(image_dir, image.name), 'wb+') + for chunk in image.chunks(): + destination.write(chunk) + destination.close() + + tik = time.time() + imagepath = URL_PORT + '/static/ImgCensorUpload/' + image.name + + nswf_result = nsfw_estimator.infer(os.path.join(image_dir, image.name)) + + result['code'] = 0 + result['msg'] = 'success' + result['imgpath'] = imagepath + result['results'] = nswf_result['results'] + result['elapse'] = round(time.time() - tik, 2) + + json_str = json.dumps(result, ensure_ascii=False) + + return HttpResponse(json_str) + else: + result['code'] = 3 + result['msg'] = 'Invalid HTTP Method' + result['data'] = None + + json_result = json.dumps(result, ensure_ascii=False) + + return HttpResponse(json_result) diff --git a/cv/static/img/portfolio-1.png b/cv/static/img/portfolio-1.png index 0ee2d55..9545be4 100644 Binary files a/cv/static/img/portfolio-1.png and b/cv/static/img/portfolio-1.png differ diff --git a/cv/static/img/portfolio-3.png b/cv/static/img/portfolio-3.png index 169896a..74ed314 100644 Binary files a/cv/static/img/portfolio-3.png and b/cv/static/img/portfolio-3.png differ diff --git a/cv/templates/index.html b/cv/templates/index.html index 89fb20e..f64e2d9 100644 --- a/cv/templates/index.html +++ b/cv/templates/index.html @@ -150,7 +150,7 @@

Research Achievements

- + diff --git a/cv/templates/mcloud.html b/cv/templates/mcloud.html index 7fac914..d6b98b0 100644 --- a/cv/templates/mcloud.html +++ b/cv/templates/mcloud.html @@ -541,8 +541,9 @@
We're on social networks

Copyright © LucasX. All rights reserved.

- Designed by LucasX. + Designed by LucasX.
+

Powered by XCloud

diff --git a/cv/templates/nsfw.html b/cv/templates/nsfw.html new file mode 100644 index 0000000..b3220c5 --- /dev/null +++ b/cv/templates/nsfw.html @@ -0,0 +1,71 @@ + + + + + Porn Image Recognition + {# {% load static %}#} + + + + + + + + +

Porn Image Recognition

+
+
{% csrf_token %} +
+ +
+ +
+ +
+
+

The image is , AI takes + 0 seconds! +

+
+ + + + + + \ No newline at end of file diff --git a/cv/urls.py b/cv/urls.py index b47e43c..f3efbe5 100644 --- a/cv/urls.py +++ b/cv/urls.py @@ -11,6 +11,8 @@ # path('sdr', views.sdr, name='sdr'), url('fbpview', views.fbp_view, name='fbpview'), url('fbp', views.fbp, name='fbp'), + url('nsfwview', views.nsfw_view, name='nsfwview'), + url('nsfw', views.nsfw, name='nsfw'), url('skinview', views.skin_view, name='skinview'), url('mcloud/skin', views.rec_skin, name='recskin'), url('detectface', views.detect_face, name='detectface'), diff --git a/cv/views.py b/cv/views.py index e6e90ca..18eb617 100644 --- a/cv/views.py +++ b/cv/views.py @@ -41,6 +41,15 @@ def skin_view(request): return render(request, 'skin.html') +def nsfw_view(request): + return render(request, 'nsfw.html') + + +def nsfw(request): + from cv import controllers + return controllers.upload_and_rec_porn(request) + + def detect_face(request): """ face detection