Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am developing an application for a project in React Native for which I need the following.

What I want to do in this code is that when a photo of a crop is clicked from the camera, by analyzing that photo, the information about where the insects and diseases are present in that crop is displayed in detail.

What I have tried:

JavaScript
//CameraScreen.js
import React, { useEffect, useRef, useState } from 'react';
import { Camera } from 'expo-camera';
import { shareAsync } from 'expo-sharing';
import * as MediaLibrary from 'expo-media-library';
import { SafeAreaView, View, Image, Button, StyleSheet,TouchableOpacity } from 'react-native';

export default function CameraScreen({ navigation }) {
  const cameraRef = useRef();
  const [hasCameraPermission, setHasCameraPermission] = useState();
  const [hasMediaLibraryPermission, 
         setHasMediaLibraryPermission] = useState();
  const [photo, setPhoto] = useState();

  useEffect(() => {
    (async () => {
      const cameraPermission = await Camera.requestCameraPermissionsAsync();
      const mediaLibraryPermission = 
              await MediaLibrary.requestPermissionsAsync();
      setHasCameraPermission(cameraPermission.status === "granted");
      setHasMediaLibraryPermission
         (mediaLibraryPermission.status === "granted");
    })();
  }, []);

  let takePic = async () => {
    let options = {
      quality: 1,
      base64: true,
      exif: false,
    };
    let newPhoto = await cameraRef.current.takePictureAsync(options);
    setPhoto(newPhoto);
  };

  if (photo) {
    let sharePic = () => {
      shareAsync(photo.uri).then(() => {
        setPhoto(undefined);
      });
    };
    let savePhoto = () => {
      MediaLibrary.saveToLibraryAsync(photo.uri).then(() => {
        setPhoto(undefined);
      });
    };
    return (
      <SafeAreaView style={styles.container}>
        <Image style={styles.preview} 
        source={{ uri: "data:image/jpg;base64," + photo.base64 }} />
        <Button title="Share" onPress={sharePic} />
        {hasMediaLibraryPermission ? <Button title="Save" 
         onPress={savePhoto} /> : undefined}
        <Button title="Discard" onPress={() => 
            setPhoto(undefined)} />
      </SafeAreaView>
    );
  }

  return (
    <Camera style={styles.container} ref={cameraRef}>
     
      {/* Add a circular button at the bottom */}
      <View style={styles.circleButtonContainer}>
        <TouchableOpacity style={styles.circleButton} 
            onPress={takePic}>
          <View style={styles.circleButtonBorder} />
        </TouchableOpacity>
      </View>
    </Camera>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  buttonContainer: {
    backgroundColor: 'transparent',
    alignSelf: 'center',
    marginBottom: 20, // Adjust the margin as needed
  },
  circleButtonContainer: {
    position: 'absolute',
    bottom: 20, // Adjust the bottom value as needed
    alignSelf: 'center',
  },
  circleButton: {
    width: 60,        // Adjust the width to make it circular
    height: 60,       // Same as the width for a circle
    borderRadius: 30, // Half of the width to make it circular
    backgroundColor: 'white', // Change the color as desired
    alignItems: 'center',
    justifyContent: 'center',
  },
  circleButtonBorder: {
    width: 56,  // Adjust the width to make it slightly smaller 
                // than the button
    height: 56, // Same as the width
    borderRadius: 28, // Half of the width
    borderWidth: 2, // Border width
    borderColor: 'black', // Border color
    position: 'absolute',
  },
  preview: {
    alignSelf: 'stretch',
    flex: 1,
  },
});
Posted
Updated 6-Nov-23 1:20am
v2
Comments
OriginalGriff 28-Oct-23 6:31am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
[no name] 28-Oct-23 12:34pm    
What you've shown has absolutely nothing to do with what's "in the picture". You should be looking into / at "object recognition".

1 solution

You have a lot of hard work ahead of you. You will need to incorporate a library capable of working with object detection and use that. You are probably going to have to train your own detection model. Getting started with object detection in react is given as an example here. Build an object classification app with TensorFlow.js and React Native - LogRocket Blog[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900