2. Materials and Methods
2.1. App Conceptualization and Development Process
The concept of the Fruit Harvest Helper came from doing some research and identifying what agricultural research tasks needed to be solved. A research study conducted in 2015 by Karkhile and Ghuge showcased a multi-purpose precision agriculture tool. The tool was an Android application that helped inform farmers to increase their profits, giving them information such as the current weather in their area, news updates, and recent market trade deals. This mobile application demonstrated many different ways mobile applications could be used for precision agriculture and clearly explained how mobile computing can help farmers out in this new age [
10]. As a logical progression of reading about this, NNU’s Robotics Vision Lab became interested in developing a mobile application for precision agriculture.
Doing more research led to the discovery that fruit yield monitoring technologies were an area that needed to be studied more. There were some existing mobile apps for fruit yield estimation, though, which included apps that detected fruits such as citrus, kiwis, and apples. The mobile application for kiwi detection, called the KiwiDetector, was an Android application that used a few different deep-learning approaches to quickly and accurately detect kiwis for kiwi yield estimation [
11]. Another existing application was the app Qian et al. developed for Fuji apple yield estimation, as discussed earlier [
6]. Learning about these mobile applications helped narrow the focus for deciding on what type of research would be done. Although there was already an apple yield estimation tool that detected and estimated the Fuji apple variety, no research on mobile applications for the Pink Lady variety was found. In noticing this, it was clear that predicting the apple yield for Pink Lady apples with a mobile device was an agricultural task that needed solving.
Since predicting apple yield for Pink Lady apples with a mobile device was something farmers still needed, the NNU team set out to develop a mobile application for this purpose. In order to be a convenient tool for farmers, this mobile application would eventually need an accurate enough algorithm and would have to be publicly available for them to download. A relatively simple image-processing algorithm was chosen for this study as a starting place for Pink Lady apple detection. With the transition to a more advanced detection algorithm in the future, the Fruit Harvest Helper will be able to save farmers time and costs. The Fruit Harvest Helper will also increase farmers’ productivity and give them more accurate fruit yields, improving decision-making in agricultural practices. Given that there is a practical need for a Pink Lady apple yield estimation tool, this is definitely a technology that farmers would be interested in downloading and adopting for use in their orchards.
The last step before development began was picking the platform(s) for creating the mobile app. A study on mobile applications for agriculture shows that most of the apps generated come from iOS and Android platforms. After all, these are the two most commonly used platforms, so this makes sense. Some of the applications this study included were farming apps for business and financial data, pests and diseases, agricultural machinery, and farm management. According to the survey, in 2016, there were 91 iOS and 69 Android mobile applications for farm management, which is the category fruit yield estimation falls under [
12]. Considering that iOS was the more popular choice for mobile apps in the farm management category, NNU’s Robotics Vision Lab initially developed a mobile application only for iOS devices. Also, creating an application for one platform was an easier task. This first mobile application for iOS was designed with a blossom detection algorithm to estimate the apple yield for an orchard. Once this mobile application was functional, there were still many improvements to the blossom detection method. Hence, the researchers switched to apple detection before making the various improvements to the blossom detection algorithm [
8]. The researchers also made the change of developing for the Android platform. The reason for this was to reach more farmers in need. According to a study conducted by the University of Salamanca, Samsung smartphones led in worldwide sales for both years studied, 2012 and 2013 [
13]. Given that more farmers would likely have an Android application, the scope of the research changed, leading the researchers to develop an Android mobile application, too, in this study. Using React Native, the researchers began to develop the mobile application for iOS and Android. Look at
Figure 1 to see this application being used.
In the development phase of the Fruit Harvest Helper app, the user interface was made using the React Native framework since this was a convenient option. React Native is a framework built directly on top of the JavaScript programming language used to create cross-platform mobile applications. As the front end took shape, the main functionality, or the back end, began to get integrated into the application, a task executed separately for the iOS and Android platforms. The iOS application relied on C++ code for the back end, whereas the Android counterpart used a combination of Java and C++. Despite these differences, both platforms shared the same OpenCV C++ apple detection algorithm. To provide context, OpenCV is a widely used machine vision library that performs the image processing component of the application. The performance of this library was reliable across both platforms. The researchers used the latest version of Visual Studio Code for the development phase.
Once the development phase for mobile applications was complete, the researchers had to test the app to see how well the product functions. The researchers took a dataset of images of mature Pink Lady apple trees a few weeks before harvest. These were the images they analyzed for testing. This testing phase aimed to verify the application’s functionality by evaluating the apple detection algorithm outside the application in a separate environment. Testing the algorithm outside the mobile application made running it way quicker, allowing the researchers to fine-tune the algorithm and evaluate its accuracy in a fraction of the time. After testing, it was determined that the algorithm wasn’t effective enough for farmers to use for apple yield estimation and still required improvements.
2.2. Algorithm Development
The Pink Lady apple detection algorithm created for the Fruit Harvest Helper implemented a color ratio-based image segmentation algorithm. This color ratio-based approach was one of many paths that could have been taken to attempt to detect apples in images of apple trees. Studies in the past have explored many different apple detection algorithms, such as the color index-based image segmentation method presented a few years ago in 2022. Each algorithm or approach has strengths and limitations [
14].
Like any algorithm, the color ratio-based approach the researchers utilized is a sequence of operations. Before processing, the algorithm received an image of the user’s choice. Once the image was obtained, the algorithm underwent a series of steps to detect apple clusters in the apple tree image, as shown in
Figure 2. First, the researchers made a copy of the input image, which all of the calculations would performed on. Using the duplicate image would preserve the input image for when it needed to be displayed. To copy the original image and then process that copy, OpenCV C++ was used. The OpenCV library represented the Pink Lady apple tree image as a multi-dimensional matrix of pixels containing a dimension for rows, columns, and color channels. The next operation consisted of splitting the image into three color channels: Blue, Green, and Red. Blue, Green, Red, or BGR is the format in which OpenCV stores images. Separating the image into color channels would allow each color component to be analyzed individually, providing more information to work with. This separation would be crucial for getting the values used in the primary step of the algorithm.
Now that the image was split into color channels, the researchers converted the copied image to grayscale. So far, the copied image has been stored as a grayscale image and as three distinct color channels. The researchers then converted each color channel to grayscale to compare the color channel intensities to the intensity of the entire input image. The result of comparing intensities was a ratio corresponding to each color channel, indicating how much of a particular color was in the image. These ratios formed the heart of the apple detection algorithm since they made up the color ratio evaluation equations that classified every pixel as an apple or not an apple, which was the next step in the algorithm. In the step of classifying the image pixels, each pixel was iterated through and classified based on the weighted evaluation equations. It should also be noted that the researchers weighted these ratios within the equations. The researchers empirically selected and fine-tuned these weights through a lot of testing to achieve the optimal detection of apples across the image dataset. As pointed out in a research article discussing fine-tuning hyperparameters, a machine-learning algorithm’s hyperparameters depend on the dataset used for fine-tuning them [
15]. This means that the hyperparameters for the image segmentation algorithm would likely function best with the dataset the researchers collected, and further testing on multiple Pink Lady apple tree orchards would need to be carried out to figure out the algorithm’s effectiveness across different datasets.
After the classification of pixels, the image segmentation results still needed to be refined and displayed. The process of refining an algorithm’s results can be described as post-processing. The post-processing operations of the Fruit Harvest Helpers algorithm were applied to the binary mask generated from the last step. One of the post-processing operations was a morphological opening. A morphological opening is a widespread image processing technique used to filter out small white regions surrounding identified apple clusters, smooth the edges of clusters, and remove noise. In a previous study by Mat Said et al., researchers illustrated the effectiveness of using a morphological opening in image processing tasks. The research team explained that the morphological opening operation erodes and dilates the input image. This operation uses a predefined structuring element to find the neighborhood of pixels that will be considered [
16]. While this is how morphological openings work in general, this is also how this operation functions in the post-processing stage of the apple detection algorithm. In the next step, contours that were too small to represent apple regions were filtered out to reduce some false detections or false positives. A minimum contour size hyperparameter was set and fine-tuned to accomplish this task. Finally, the remaining contours represented the significant apple clusters so these contours could be drawn onto the copied image. These contours were drawn where the algorithm "thought" the apples were based on the steps just described. In the drawing step, green bounding boxes were put onto the image, highlighting regions identified as apple clusters for visualization.
Initially, the image segmentation algorithm was developed with the latest version of MatLab. Developing in MatLab was done to facilitate the creation of the apple evaluation equations since MatLab is made for math-based tasks like that. Later, the MatLab algorithm was converted to OpenCV C++ to be placed inside the Fruit Harvest Helper mobile app. Utilizing C++ enabled the detection algorithm to work with iOS and Android platforms.
When gathering data, the researchers took pictures of apple trees at Williamson’s apple orchard in October 2023. An Android device was used to capture images from both the west-facing and east-facing sides of the Pink Lady apple trees. In total, 40 trees were involved in the research dataset. Unfortunately, 39 east- and 34 west-side images were collected due to unforeseen circumstances. Although challenges existed with image collection, the researchers refined the algorithm through tests using the available photos. This fine-tuning significantly improved how well the algorithm could detect apples based on color. The algorithm now accurately identifies apples by focusing on areas of the apple tree images. Having a fully functional apple detection algorithm is a big step forward in creating a reliable mobile app for estimating apple yields for Pink Lady orchards.
2.3. Front-end Development
According to a study by Brambilla et al., when designing any application, two large-picture things should be considered: the front end and the back end. An application’s front end is responsible for its appearance and feel. Having a well-thought-out user interface (UI) ensures that the user of the technology has a positive experience [
17]. Furthermore, an excellent front end should keep the users engaged and satisfied by serving its purpose and nothing else. In the case of the Fruit Harvest Helper app, creating a UI tailored to farmers was essential for keeping them satisfied as they used the app. Tailoring the app to them involved making the design simple and easy to use since farmers likely prefer functionality over complexity. As depicted in
Figure 3, the layout of the Fruit Harvest Helper mobile application was straightforward and lacked any unnecessary features or elements.
To develop the front end, the researchers used the React Native framework, which takes an approach to simultaneously developing both iOS and Android platforms. Designing both applications at the same time was much more efficient than individually building out the front ends. Considering what design farmers would want, they first planned the information layout for display. By incorporating the same elements from the existing blossom detection app, created by the NNU Robotics Vision Lab previously, the process of designing the UI for this new application became easy.
During the design phase of the UI, the researchers added four main components to display to the farmers by using React Native. Firstly, showing the selected or taken image of a Pink Lady apple tree helps farmers confirm their image choice, providing a sense of direction for them. The processed image, generated through the process of the algorithm analysis of the photo, offers farmers valuable insights into the apple detection results. Therefore, it was also chosen for display. These two elements are displayed in vertically aligned boxes with the regular image placed above the processed one. Also, the UI shows the number of apples detected as an output measure for farmers to estimate the apple yield for their orchards. Before they could calculate an estimate for apple yield, though, additional calculations like applying a correlation between detected and actual Pink Lady apples would be necessary. The researchers found a correlation in this study, which will be discussed later on, but at the current stage of the application, it can’t be applied directly to this output of apples number. Considering there are different varieties of apple trees, creating a correlation for each specific variety being analyzed would be vital for precise estimations across these different types of trees. Lastly, there is a button to select new images for processing. After clicking the button, an easy-to-use pop-up interface provides options to choose images from the photo library, capture a photo, and cancel the action. Once an apple tree image is selected, it gets transmitted to the back end, where it is processed. The UI is then instantly refreshed to show the altered Pink Lady apple tree image and the number of apple clusters identified and displayed.
2.4. iOS Back-end Development
The functionality that lies at the core of an application called the back end of an application, serves the purpose of handling the user’s input or selections and then processing that input to generate an output. In the context of the Fruit Harvest Helper mobile app, which focuses on detecting apples, a farmer picks an image of a Pink Lady apple tree, which is the user selection. After processing this image on the back end, a processing image along with a tally of the identified apple clusters are returned to the user on the front end, as just discussed. To bring this back-end functionality to life, the researchers utilized the C++ programming language, C++ native libraries, and the OpenCV library for processing.
The Fruit Harvest Helper’s architectural design followed a pattern described in a research study by Thakur and Pandey, the Model View Controller (MVC) pattern, which is an embraced approach in software engineering. The MVC pattern segmented the application into three parts: the Model managed input data, the View handled the front-end presentation for users or farmers, and the Controller processed data behind the scenes. This separation of concerns made the code more modular and maintainable for future researchers who make additions or changes to this application [
18]. Enabling communication between React Native and the C++ back end involved integrating Djinni, a deprecated tool that can be used for communication between the front end and back end in React Native applications. The researchers initially selected Djinni for its ability to generate a back-end file where the functionality for both iOS and Android could be written to process apple tree images. Unfortunately, the researchers faced challenges in making this method compatible with Android devices, but it still worked as a solution for the iOS back end.
To overcome the difficulties of sending the Pink Lady apple tree images to the C++ back end using Djinni, the research team developed a workaround. This workaround was just a matter of saving the apple tree image in the cache of the iOS device on the React Native front end and later retrieving it by the C++ back end through the use of the iOS device’s file system. Once the image was received and it was confirmed, the OpenCV C++ image segmentation algorithm was applied, which used the machine learning techniques outlined in the algorithm section. The edited or processed image and a count of the detected Pink Lady apples were then sent back to the front end to display to the farmers.
2.5. Android Back-end Development
The development of the Android back end consisted of using a mixture of Java and C++. Java is the native back-end language for Android, meaning it had to be used, and C++ needed to be used to use the same color ratio-based OpenCV C++ algorithm. Recalling the iOS app created a pathway to send information from the application’s front end to the back end using Djinni. Still, the researchers needed help to make this method successful for Android. After exploring this option enough, a more well-known method of communicating with the Android back end was tried. Typically, Android Native Modules can provide back-end functionality for Android apps when working with the React Native framework. By using a more popular method, documentation was easier to find and, therefore, followed for the creation of the Android back end. Android Native Modules provide a back-end environment written in Java that can be used for mobile applications.
The React Native front end establishes a connection with an Android Native Module by calling a Java function that developers create within the module. Setting up this connection required the researchers to undergo various configuration steps. Initially, a JavaScript wrapper was made to wrap around the module on the React Native front end. Also, methods that needed to be called in the Android Native Module from the front end had to be labeled as callable so they could be found when an image was selected for processing. The last step was registering the native module for usage within the application, making it recognizable to Android devices. These configuration steps marked the first phase of handling the image processing for Android.
The color ratio-based image segmentation algorithm was written in OpenCV C++, which needed to work for both iOS and Android. In order to get this algorithm to be compatible with Android, communication between Java and C++ had to be made, which brought about a set of challenges. The Java Native Interface (JNI) was a viable solution because it allows Java to talk back and forth with other languages, such as C++. Configuring JNI required adjusting the Android applications build process to get these two languages to cooperate correctly. The Android Native Module had to be able to interact with C++, too. Configuring the Android Native Module to interact with the C++ code meant writing more Java code to get the native module to recognize that C++ code was available. This configuration allowed the native module to establish a communication channel with C++ for sending the original apple tree image and receiving the processed image. In the C++ image processing file, a JNI function was implemented to get the communication channel working on the C++ side. The C++ could then accept the sent-over image data, process it, and relay it back to the Java side. Once the processed image reached Java, Java passed the image over to React Native to display it on the UI.
To finish setting up the Android back end, OpenCV had to be installed and integrated into the current setup. Installing OpenCV was straightforward since this involved downloading OpenCV version 4.6.0 for Android. The researchers downloaded and used this version for iOS, too. However, once OpenCV was installed, the Android build process had to be configured again so Android devices could recognize the OpenCV library. When OpenCV started working within the setup, the apple detection algorithm was copied from the iOS C++ file and pasted into the Android C++ file. Although this meant the code for the algorithm was used twice, this solution saved time and was able to process images just fine. One drawback of this entire solution for image processing on Android was that JNI worsened processing times.