Article originated from https://medium.com/@hafiqiqmal93/blurry-image-detection-in-laravel-4c91168e00f1
Crucial aspect of user experience, storing blurry images is significantly detract from the quality of a website or application. This article delves into how you can detect and manage blurry images using Laravel with help of Python and OpenCV, ensuring the application’s media remains sharp and engaging.
Blurry images are more than just a visual nuisance; they can undermine the professionalism of your website or app. In e-commerce, real estate listings, online galleries or any platform where image quality is paramount, ensuring clarity is essential. The challenge lies in detecting blurriness programmatically.
Laravel can be paired with Python to create an effective solution for this problem. By leveraging Laravel’s file validation alongside a Python script utilizing OpenCV, developers can seamlessly integrate blur detection into their file upload processes.
The detection of blurry images involves analyzing the image’s sharpness. This is typically done using the Laplacian operator, a mathematical tool used in image processing. The Laplacian operator measures the rate at which pixel intensity changes, and a lower variance of the Laplacian indicates a blurrier image.
In Laravel, we can create a custom validation rule to check for image blurriness. This rule executes a Python script that uses the Laplacian operator to determine the sharpness of the image. Let’s break down the process:
Install PIP (Ubuntu) :
sudo apt install python3-pip
Install OpenCV using PIP
pip3 install opencv-python
You might want to consider to install under **www-data** user if your application runs under **www-data**. If Yes, follow below commands to install
<p>sudo mkdir /var/www/.local<br> sudo mkdir /var/www/.cache<br> sudo chown www-data.www-data /var/www/.local<br> sudo chown www-data.www-data /var/www/.cache<br> sudo -H -u www-data pip3 install opencv-python</p>
<p>import sys<br> import cv2</p> <p>def get_image_laplacian_value(image_path):<br> image = cv2.imread(image_path)<br> gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<br> return cv2.Laplacian(gray_image, cv2.CV_64F).var()</p> <p>if <strong>name</strong> == "<strong>main</strong>":<br> if len(sys.argv) != 2:<br> sys.exit(1)<br> image_path = sys.argv[1]<br> laplacian_value = get_image_laplacian_value(image_path)<br> print(laplacian_value)</p>
<p>class ImageBlurDetectionRule implements ValidationRule<br> {<br> public function validate(string $attribute, mixed $value, Closure $fail): void<br> {<br> if ( ! $value instanceof UploadedFile) {<br> return;<br> }<br> // ignore if not image<br> if ('' !== $value->getPath() && ! in_array($value->guessExtension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'])) {<br> return;<br> }<br> // get real path for the file<br> $path = $value->getRealPath();<br> $command = escapeshellcmd(config('image.python_path') . " blur_detection.py '{$path}'");<br> $result = Process::path(base_path('scripts'))->run($command);<br> if ( ! $result->successful()) {<br> return;<br> }<br> if (trim($result->output()) < 100) {<br> $fail(__('Blur image are not accepted. Please make sure your :attribute image is clearly visible.'));<br> }<br> }<br> }</p>
The integration of Laravel with a Python script for blur detection works in a seamless manner, offering a sophisticated yet straightforward approach to ensuring image quality. Here’s how the process unfolds:
When a user uploads an image to the Laravel application, the custom validation rule (ImageBlurDetectionRule) is triggered.
This rule first checks if the uploaded file is indeed an image by verifying its extension. If the file is not an image, the process stops here.
If the file is an image, the rule then calls a Python script, blur_detection.py. The image's path is passed to this script as a command-line argument.
The script calculates the variance of the Laplacian, which serves as a measure of the image’s sharpness. A lower variance indicates a blurrier image.
If the image is too blurry (ex: the Laplacian variance is below the threshold), the validation rule fails and the user receives a message indicating that the image is blurry and should be checked.
By preventing the upload of low-quality, blurry images, this solution enhances the overall user experience. Users are prompted to only upload clear, high-quality images, which maintains the visual standard of the application.
Proses ini sangat boleh disesuaikan. Pembangun boleh melaraskan ambang untuk kekaburan mengikut keperluan khusus aplikasi mereka. Ambil perhatian bahawa, ambang adalah berdasarkan pemerhatian anda. Untuk penggunaan awal, mungkin memerlukan ML untuk menentukan ambang. Selain itu, penyepaduan Python dalam Laravel membolehkan pengembangan selanjutnya ke dalam teknik pemprosesan imej yang lebih maju, menawarkan penyelesaian yang fleksibel dan mantap untuk mengurus kualiti imej.
Menggabungkan fungsi ini dalam aplikasi Laravel anda meningkatkan pengalaman pengguna dengan menghalang muat naik imej berkualiti rendah. Ini amat berguna dalam senario di mana kejelasan imej adalah kritikal, seperti portfolio dalam talian, katalog produk atau gambar profil pengguna.
Ambang untuk kekaburan boleh dilaraskan mengikut keperluan khusus. Selain itu, penyepaduan Python dalam Laravel menawarkan fleksibiliti untuk menggabungkan teknik pemprosesan imej yang lebih maju jika diperlukan.
Gabungan Laravel dan Python untuk mengesan imej kabur adalah penyelesaian yang berkuasa. Ia bukan sahaja memastikan kualiti visual aplikasi anda tetapi juga meningkatkan keseluruhan pengalaman pengguna. Dengan pendekatan ini, pembangun boleh mengekalkan standard yang tinggi untuk kandungan media, menyumbang kepada kehadiran dalam talian yang lebih digilap dan profesional.
Sudahkah anda cuba melaksanakan penyelesaian ini dalam projek Laravel anda? Kongsi pengalaman anda dan sebarang cerapan yang anda perolehi dalam ulasan di bawah. Mari kita terus meningkatkan standard pembangunan web bersama-sama!
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!