B4J Question 1st PyBridge try: alfa-test

peacemaker

Expert
Licensed User
Longtime User
Hi, All and @Erel ,

Just tried for the first time your PyBridge draft. B4J 10.0
Compiling at line:
Dim correctClassesNames As Map = jo.InitializeStatic("anywheresoftware.b4a.randomaccessfile.RandomAccessFile").GetField("correctedClasses")
gives the error:
Just alfa-test...
Any updates in jRandomAccessFile lib?
 

Attachments

  • TempDownload.png
    17.8 KB · Views: 154
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Ok can be... recognition counting persons
it would be something similar to this (not tried as I dont have hundreds of images to train it)

B4X:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
import numpy as np
from keras.utils import Sequence
from keras.preprocessing.image import load_img, img_to_array

class DataGenerator(Sequence):
    def __init__(self, image_filenames, labels, batch_size, img_size):
        self.image_filenames = image_filenames
        self.labels = labels
        self.batch_size = batch_size
        self.img_size = img_size
    def __len__(self):
        return int(np.ceil(len(self.image_filenames) / self.batch_size))
    def __getitem__(self, idx):
        batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]
        images = [img_to_array(load_img(file_name, target_size=self.img_size)) for file_name in batch_x]
        return np.array(images), np.array(batch_y)

# Load dataset
import pandas as pd
def read_training_data(path_to_labels_csv):
    global model
    data = pd.read_csv(path_to_labels_csv)
    image_filenames = data['filename'].values
    labels = data['count'].values
    # Create data generator   batch size < number of pics
    train_gen = DataGenerator(image_filenames, labels, batch_size=32, img_size=(128, 128))
    # Train the model
    model.fit(train_gen, epochs=10, validation_data=validation_gen)
    return f"Finished Training""

# Function to preprocess and predict the count of people in a new image
def predict_people_count(model, image_path):
    # Load and preprocess the image
    image = load_img(image_path, target_size=(128, 128))
    image_array = img_to_array(image)
    image_array = np.expand_dims(image_array, axis=0)  # Add batch dimension
    image_array = preprocess_input(image_array)
    # Make prediction
    prediction = model.predict(image_array)
    count = int(np.round(prediction[0][0]))  # Round and convert to integer
    return count

# Example usage
def count_people(image_path):
    people_count = predict_people_count(model, image_path)
    return f'I see {people_count} people in the image.'

you would have a csv (where count is the number of people in the image and filemane is the image url)

filename,count
c:\xxx\img1.png,0
c:\xxx\img2.png,0
...
c:\xxx\img100.png,1
...
c:\xxx\img200.png,2

and call from B4J like
B4X:
    Wait for (Py.Utils.RunCode("read_training_data",array("path-to-csv"), Code).Fetch) Complete(Result As PyWrapper)
    Log(Result.Value.As(String))
...
    Wait for (Py.Utils.RunCode("count_people",array("path-to-new-image"), Code).Fetch) Complete(Result As PyWrapper)
    Log(Result.Value.As(String))

You would have exxtra code to save/load the model so you could just use for new images after training it.
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
Wow, in this example... the images will have zero people at first and then at same scene of image (3-100) different people and changing their number +1 every time... ??

also... this command: "target_size=(128, 128)"
What means ? are pixels... array ?
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
yes series of pics with 0,1,2,3...people does not have to be same scene, only the number of people.

target_size is the image size the software works on, will resize images on load,
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
yes, you can change it to a larger size, but the training time will increase (128 x 128 > 512 x 512 at least 16 times longer to train). (Big commercial models take millions of GPU hours to train, and they dont use consumere GPU either)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Another good lib is pandas
Example
csv file like this (needs the header line to work by names)
B4X:
name,age,gender
fred,24,male
julie,20,female
mike,44,male
susan,55,female

Python Code
B4X:
import pandas as pd
def to_a_list(col_name,filename,query):

    # Load CSV file into a DataFrame
    df = pd.read_csv(filename)
    # select data on contents of query
    screened_data = df.query(query)

    # Get the data in the 'column_name' column as a list
    column_data = screened_data[col_name].values.tolist()

    # return the list
    return column_data

B4J code (ignoring setup of PyBridge)
B4X:
Private Sub Button1_Click
' the query is "age> 30  & gender=='female'"
    wait for (csvToList(File.DirAssets,"testfile.csv","age>30 & gender=='female'")) Complete (rr As List)
    'print out the list of csv records founds
    For Each n As String In rr
        Log(n) 
    Next
End Sub

Sub csvToList(path As String,fname As String, criteria As String)As ResumableSub
    Dim cols As List = Array("name","gender","age")  ' these are the columns I want in this order, query cols dont have to be in it
' execute the python script from DirAssets called csv.py
    Wait for (Py.Utils.RunCode("to_a_list", Array(cols, File.GetUri(path,fname), criteria), File.ReadString(path,"csv.py")).Fetch) Complete(Result As PyWrapper)
' return the list
    Return Result.Value.As(List)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…