I want to make a program that can detect masked faces other than using OpenCV in Java. is there any other way I can use?
Here’s the code I’ve created
`private void btnuploadActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnuploadActionPerformed
JFileChooser chooseFile = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(“3 Extensiosn supported”, “jpg”, “png”, “jpeg”);
chooseFile.setFileFilter(filter);
int terpilih = chooseFile.showOpenDialog(this);
if (terpilih == JFileChooser.APPROVE_OPTION) {
File file = chooseFile.getSelectedFile();
String getGambarTerpilih = file.getAbsolutePath();
JOptionPane.showMessageDialog(null, “Gambar berhasil diinput”);
ImageIcon icon = new ImageIcon(getGambarTerpilih);
try {
BufferedImage colorImage = ImageIO.read(file);
BufferedImage grayscaleImage = new BufferedImage(colorImage.getWidth(), colorImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
int jml = 0;
int grayLevel = 0;
Color color;
for (int y = 0; y < colorImage.getHeight(); y++) {
for (int x = 0; x < colorImage.getWidth(); x++) {
int rgb = colorImage.getRGB(x, y);
color = new Color(rgb);
grayLevel = (int) (0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue());
int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;
grayscaleImage.setRGB(x, y, gray);
jml += grayLevel;
}
}
int rata = jml / (colorImage.getHeight() * colorImage.getWidth());
for (int y = 0; y < colorImage.getHeight(); y++) {
for (int x = 0; x < colorImage.getWidth(); x++) {
int rgb = colorImage.getRGB(x, y);
color = new Color(rgb);
int gl = 0;
grayLevel = (int) (0.299 * color.getRed() + 0.587 * color.getGreen() + 0.114 * color.getBlue());
if (grayLevel > rata) {
gl = 255;
} else {
gl = 0;
}
int gray = (gl << 16) + (gl << 8) + gl;
grayscaleImage.setRGB(x, y, gray);
}
}
Image resizedGrayscaleImage = grayscaleImage.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
label.setIcon(new ImageIcon(resizedGrayscaleImage));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Gagal mengubah gambar menjadi grayscale!");
}
}
}`
Muhammad Abyan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.