notAI-tech / NudeNet

Lightweight nudity detection
https://nudenet.notai.tech/
GNU Affero General Public License v3.0
1.69k stars 334 forks source link

img byte array #152

Closed DumbBoy10 closed 2 weeks ago

DumbBoy10 commented 2 weeks ago

How do I parse an img byte array into the detect function.

I have an image from form and I want to check that image before I save it. How do I achieve that.

I all ways end with this error: 'ValueError: please make sure the image_path is str or np.ndarray or bytes'

image = self.cleaned_data.get('image') #get image from that django form
detector = NudeDetector()
test = detector.detect(image)
print(test)
bedapudi6788 commented 2 weeks ago

@DumbBoy10 can you do a pickle.dump of the bytearray you are passing to the detect function and post here? I will check and add support if needed.

DumbBoy10 commented 2 weeks ago

Sorry for inconvenience I just resolved it with pillow and this lines of code. Thanks for you answering.

image = self.cleaned_data.get('image')
# Convert Django UploadedFile to PIL Image
pil_image = PilImage.open(image)
# Save PIL Image to an in-memory bytes buffer
img_byte_arr = io.BytesIO()
pil_image.save(img_byte_arr, format=pil_image.format)
img_byte_arr = img_byte_arr.getvalue()
# Assuming NudeDetector can work with bytes, pass the bytes directly
detector = NudeDetector()
# If NudeDetector requires a file-like object, use io.BytesIO(img_byte_arr) instead of img_byte_arr
test = detector.detect(img_byte_arr)