diff --git a/.gitignore b/.gitignore index b6e4761..3081718 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ +# Test files + +data/vids/* +data/screen/* +data/json/* +data/*.* + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/data/model/nsfw_mobilenet2.224x224.h5 b/data/model/nsfw_mobilenet2.224x224.h5 new file mode 100644 index 0000000..40a7a57 Binary files /dev/null and b/data/model/nsfw_mobilenet2.224x224.h5 differ diff --git a/data/templates/index.html b/data/templates/index.html new file mode 100644 index 0000000..32aacb9 --- /dev/null +++ b/data/templates/index.html @@ -0,0 +1,11 @@ + + + {file_name} + +

{file_name}


+

{resolution}


+

{duration}


+
+
+ {frame_info} + \ No newline at end of file diff --git a/data/templates/one_frame.html b/data/templates/one_frame.html new file mode 100644 index 0000000..f1f86ed --- /dev/null +++ b/data/templates/one_frame.html @@ -0,0 +1,6 @@ +
+

{file_name}

+ {file_name}
+

{info_res}

+
+
\ No newline at end of file diff --git a/html_create.py b/html_create.py new file mode 100644 index 0000000..0769ce7 --- /dev/null +++ b/html_create.py @@ -0,0 +1,30 @@ +import json + + +def create(file_name): + datas = json.load(open("data/json/"+file_name+".json", "r", encoding='utf-8')) + text_no_format = open("data/templates/index.html", "r").read() + text_f_no_format = open("data/templates/one_frame.html", "r").read() + list_frame_id = datas["frames"].keys() + text_f_format = "" + for ii in list_frame_id: + res_info="" + for dd in datas["frames"][ii].keys(): + if dd == "file": + pass + else: + res_add = dd + " : " +str(round(datas["frames"][ii][dd]*100, 2))+"
" + res_info += res_add + text_add = text_f_no_format.format( + file_name=datas["frames"][ii]["file"], + info_res=res_info + ) + text_f_format += text_add + text_format = text_no_format.format( + file_name=file_name, + resolution=str(round(datas["height"]))+"x"+str(round(datas["width"])), + duration=str(round(datas["duration"], 2))+" s.", + file_vid="vids/"+file_name, + frame_info=text_f_format + ) + open("data/"+file_name+".html", "w").write(text_format) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0cda4f7 --- /dev/null +++ b/main.py @@ -0,0 +1,48 @@ +import PIL.Image as Image +from nsfw_detector import predict +import cv2 +import os +import json +import html_create + +def save_frame(screen_folder, secs, vid): + save_name = screen_folder + "/" + str(secs) + ".jpg" + fps = vid.get(cv2.CAP_PROP_FPS) + vid.set(cv2.CAP_PROP_POS_FRAMES, fps*secs) + ret, frame = vid.read() + cv2.imwrite(save_name, frame) + return save_name + +model = predict.load_model('./data/model/nsfw_mobilenet2.224x224.h5') +file_path = "./data/vids/5.mp4" +vid = cv2.VideoCapture( file_path ) +height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) +width = vid.get(cv2.CAP_PROP_FRAME_WIDTH) +fps = vid.get(cv2.CAP_PROP_FPS) +totalNoFrames = vid.get(cv2.CAP_PROP_FRAME_COUNT) +durationInSeconds = float(totalNoFrames) / float(fps) +datas = {"height":height, "width":width, "duration":durationInSeconds} +print(height, width, durationInSeconds) +screen_folder = "data/screen/" + file_path.split("/")[-1] +try: + if not os.path.exists(screen_folder): + os.makedirs(screen_folder) +except OSError: + print('Error: Creating directory of data') +save_frame(screen_folder, 1, vid) +current_sec = 1 +datas_frames = {} +while True: + if current_sec < durationInSeconds: + file_name = save_frame(screen_folder, current_sec, vid) + image = Image.open(file_name) + data_res = predict.classify(model, file_name) + data_one = data_res[list(data_res.keys())[0]] + data_one["file"] = file_name.replace("data/", "", 1) + datas_frames[str(current_sec)] = data_one + else: + break + current_sec += 5 +datas["frames"] = datas_frames +json.dump(datas, open("data/json/"+file_path.split("/")[-1]+".json", "w", encoding='utf-8'), ensure_ascii=False, indent=4) +html_create.create(file_path.split("/")[-1]) \ No newline at end of file