Есть ли способ сделать обнаружение объектов в режиме реального времени с помощью Microsoft Cognitive Service Face API?
Я экспериментирую, как сделать так, чтобы образцы когнитивных сервисов Microsoft сталкивались с API для реального времени, и я не могу найти способ, я попытался смешать коды из фотоаппарата, чтобы он делал прямоугольник на обнаруженных лицах, а затем из идентификационной задачи из когнитивного примера Microsoft как это, но это не будет работать.
public class RealtimeCameraActivity extends Activity {
boolean detected;
String mPersonGroupId;
private final PermissionsDelegate permissionsDelegate = new PermissionsDelegate(this);
private boolean hasCameraPermission;
private CameraView cameraView;
private RectanglesView rectanglesView;
private FotoapparatSwitcher fotoapparatSwitcher;
private Fotoapparat frontFotoapparat;
private Fotoapparat backFotoapparat;
FaceListAdapter mFaceListAdapter;
PersonGroupListAdapter mPersonGroupListAdapter;
private class IdentificationTask extends AsyncTask<UUID, String, IdentifyResult[]> {
private boolean mSucceed = true;
String mPersonGroupId;
IdentificationTask(String personGroupId) {
this.mPersonGroupId = personGroupId;
}
@Override
protected IdentifyResult[] doInBackground(UUID... params) {
// Get an instance of face service client to detect faces in image.
FaceServiceClient faceServiceClient = SampleApp.getFaceServiceClient();
try{
publishProgress("Getting person group status...");
TrainingStatus trainingStatus = faceServiceClient.getLargePersonGroupTrainingStatus(
this.mPersonGroupId); /* personGroupId */
if (trainingStatus.status != TrainingStatus.Status.Succeeded) {
// publishProgress("Person group training status is " + trainingStatus.status);
mSucceed = false;
return null;
}
// publishProgress("Identifying...");
// Start identification.
return faceServiceClient.identityInLargePersonGroup(
this.mPersonGroupId, /* personGroupId */
params, /* faceIds */
1); /* maxNumOfCandidatesReturned */
} catch (Exception e) {
mSucceed = false;
// publishProgress(e.getMessage());
// addLog(e.getMessage());
return null;
}
}
@Override
protected void onPreExecute() {
;
}
@Override
protected void onProgressUpdate(String... values) {
// Show the status of background detection task on screen.a
}
@Override
protected void onPostExecute(IdentifyResult[] result) {
// Show the result on screen when detection is done.
setUiAfterIdentification(result, mSucceed);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realtime);
cameraView = (CameraView) findViewById(R.id.cameraView);
rectanglesView = (RectanglesView) findViewById(R.id.rectanglesView);
hasCameraPermission = permissionsDelegate.hasCameraPermission();
if (hasCameraPermission) {
cameraView.setVisibility(View.VISIBLE);
} else {
permissionsDelegate.requestCameraPermission();
}
frontFotoapparat = createFotoapparat(LensPosition.FRONT);
backFotoapparat = createFotoapparat(LensPosition.BACK);
fotoapparatSwitcher = FotoapparatSwitcher.withDefault(backFotoapparat);
View switchCameraButton = findViewById(R.id.switchCamera);
switchCameraButton.setVisibility(
canSwitchCameras()
? View.VISIBLE
: View.GONE
);
switchCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchCamera();
}
});
}
// Show the result on screen when detection is done.
private void setUiAfterIdentification(IdentifyResult[] result, boolean succeed) {
if (succeed) {
// Set the information about the detection result.
if (result != null) {
//android things
// try {
// Led = RainbowHat.openLedBlue();
// Led.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// } catch (IOException e) {
// e.printStackTrace();
// }
mFaceListAdapter.setIdentificationResult(result);
// if (logString.contains("Unknown Person")) {
// setLedValue(false);
// } else {
// setLedValue(true);
// }
// Show the detailed list of detected faces.
ListView listView = (ListView) findViewById(R.id.list_identified_faces);
listView.setAdapter(mFaceListAdapter);
}
}
}
private boolean canSwitchCameras() {
return frontFotoapparat.isAvailable() == backFotoapparat.isAvailable();
}
private Fotoapparat createFotoapparat(LensPosition position) {
return Fotoapparat
.with(this)
.into(cameraView)
.lensPosition(lensPosition(position))
.frameProcessor(
FaceDetectorProcessor.with(this)
.listener(new FaceDetectorProcessor.OnFacesDetectedListener() {
@Override
public void onFacesDetected(List<Rectangle> faces) {
Log.d("&&&", "Detected faces: " + faces.size());
rectanglesView.setRectangles(facess);
// Start a background task to identify faces in the image.
List<UUID> faceIds = new ArrayList<>();
for (Face face: mFaceListAdapter.faces) {
faceIds.add(face.faceId);
}
new RealtimeCameraActivity.IdentificationTask(mPersonGroupId).execute(
faceIds.toArray(new UUID[faceIds.size()]));
}
})
.build()
)
.logger(loggers(
logcat(),
fileLogger(this)
))
.build();
}
void setPersonGroupSelected(int position) {
if (position > 0) {
String personGroupIdSelected = mPersonGroupListAdapter.personGroupIdList.get(position);
mPersonGroupListAdapter.personGroupIdList.set(
position, mPersonGroupListAdapter.personGroupIdList.get(0));
mPersonGroupListAdapter.personGroupIdList.set(0, personGroupIdSelected);
setPersonGroupSelected(0);
} else {
mPersonGroupId = mPersonGroupListAdapter.personGroupIdList.get(0);
}
}
// Background task of face detection.
private class DetectionTask extends AsyncTask<InputStream, String, Face[]> {
@Override
protected Face[] doInBackground(InputStream... params) {
// Get an instance of face service client to detect faces in image.
FaceServiceClient faceServiceClient = SampleApp.getFaceServiceClient();
try{
// Start detection.
return faceServiceClient.detect(
params[0], /* Input stream of image to detect */
true, /* Whether to return face ID */
false, /* Whether to return face landmarks */
/* Which face attributes to analyze, currently we support:
age,gender,headPose,smile,facialHair */
null);
} catch (Exception e) {
return null;
}
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(String... values) {
// Show the status of background detection task on screen.
}
@Override
protected void onPostExecute(Face[] result) {
if (result != null) {
// Set the adapter of the ListView which contains the details of detected faces.
mFaceListAdapter = new RealtimeCameraActivity.FaceListAdapter(result);
ListView listView = (ListView) findViewById(R.id.list_identified_faces);
listView.setAdapter(mFaceListAdapter);
if (result.length == 0) {
detected = false;
} else {
detected = true;
}
} else {
detected = false;
}
}
}
private void switchCamera() {
if (fotoapparatSwitcher.getCurrentFotoapparat() == frontFotoapparat) {
fotoapparatSwitcher.switchTo(backFotoapparat);
} else {
fotoapparatSwitcher.switchTo(frontFotoapparat);
}
}
@Override
protected void onStart() {
super.onStart();
if (hasCameraPermission) {
fotoapparatSwitcher.start();
}
}
@Override
protected void onStop() {
super.onStop();
if (hasCameraPermission) {
fotoapparatSwitcher.stop();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissionsDelegate.resultGranted(requestCode, permissions, grantResults)) {
fotoapparatSwitcher.start();
cameraView.setVisibility(View.VISIBLE);
}
}
private class FaceListAdapter extends BaseAdapter {
List<Face> faces;
List<IdentifyResult> mIdentifyResults;
// Initialize with detection result.
FaceListAdapter(Face[] detectionResult) {
faces = new ArrayList<>();
mIdentifyResults = new ArrayList<>();
if (detectionResult != null) {
faces = Arrays.asList(detectionResult);
}
}
public void setIdentificationResult(IdentifyResult[] identifyResults) {
mIdentifyResults = Arrays.asList(identifyResults);
}
@Override
public boolean isEnabled(int position) {
return false;
}
@Override
public int getCount() {
return faces.size();
}
@Override
public Object getItem(int position) {
return faces.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(
R.layout.item_face_with_description, parent, false);
}
convertView.setId(position);
if (mIdentifyResults.size() == faces.size()) {
// Show the face details.
DecimalFormat formatter = new DecimalFormat("#0.00");
if (mIdentifyResults.get(position).candidates.size() > 0) {
String personId =
mIdentifyResults.get(position).candidates.get(0).personId.toString();
String personName = StorageHelper.getPersonName(
personId, mPersonGroupId, RealtimeCameraActivity.this);
String identity = "Person: " + personName + "\n"
+ "Confidence: " + formatter.format(
mIdentifyResults.get(position).candidates.get(0).confidence);
((TextView) convertView.findViewById(R.id.text_detected_face)).setText(
identity);
} else {
((TextView) convertView.findViewById(R.id.text_detected_face)).setText(
"Unknown person");
}
}
return convertView;
}
}
// The adapter of the ListView which contains the person groups.
private class PersonGroupListAdapter extends BaseAdapter {
List<String> personGroupIdList;
// Initialize with detection result.
PersonGroupListAdapter() {
personGroupIdList = new ArrayList<>();
Set<String> personGroupIds
= StorageHelper.getAllPersonGroupIds(RealtimeCameraActivity.this);
for (String personGroupId: personGroupIds) {
personGroupIdList.add(personGroupId);
if (mPersonGroupId != null && personGroupId.equals(mPersonGroupId)) {
personGroupIdList.set(
personGroupIdList.size() - 1,
mPersonGroupListAdapter.personGroupIdList.get(0));
mPersonGroupListAdapter.personGroupIdList.set(0, personGroupId);
}
}
}
@Override
public int getCount() {
return personGroupIdList.size();
}
@Override
public Object getItem(int position) {
return personGroupIdList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_person_group, parent, false);
}
convertView.setId(position);
// set the text of the item
String personGroupName = StorageHelper.getPersonGroupName(
personGroupIdList.get(position), RealtimeCameraActivity.this);
int personNumberInGroup = StorageHelper.getAllPersonIds(
personGroupIdList.get(position), RealtimeCameraActivity.this).size();
((TextView)convertView.findViewById(R.id.text_person_group)).setText(
String.format(
"%s (Person count: %d)",
personGroupName,
personNumberInGroup));
if (position == 0) {
((TextView)convertView.findViewById(R.id.text_person_group)).setTextColor(
Color.parseColor("#3399FF"));
}
return convertView;
}
}
как я это сделаю?