-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Merged] Added pose detection and correction
Added pose detection and correction
- Loading branch information
Showing
65 changed files
with
4,830 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
app/src/main/java/com/modarb/android/posedetection/CameraActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package com.modarb.android.posedetection | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.CompoundButton | ||
import android.widget.ImageView | ||
import android.widget.Toast | ||
import android.widget.ToggleButton | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.gms.common.annotation.KeepName | ||
import com.modarb.android.R | ||
import com.modarb.android.posedetection.Utils.CameraSource | ||
import com.modarb.android.posedetection.Utils.CameraSourcePreview | ||
import com.modarb.android.posedetection.Utils.PreferenceUtils | ||
import com.modarb.android.posedetection.posedetector.PoseDetectorProcessor | ||
import java.io.IOException | ||
|
||
@KeepName | ||
class CameraActivity : AppCompatActivity(), | ||
CompoundButton.OnCheckedChangeListener { | ||
|
||
private var cameraSource: CameraSource? = null | ||
private var preview: CameraSourcePreview? = null | ||
private var graphicOverlay: GraphicOverlay? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
Log.d(TAG, "onCreate") | ||
setContentView(R.layout.activity_camera_view) | ||
|
||
preview = findViewById(R.id.preview_view) | ||
if (preview == null) { | ||
Log.d(TAG, "Preview is null") | ||
} | ||
|
||
graphicOverlay = findViewById(R.id.graphic_overlay) | ||
if (graphicOverlay == null) { | ||
Log.d(TAG, "graphicOverlay is null") | ||
} | ||
initSetting() | ||
createCameraSource(POSE_DETECTION) | ||
handleCameraSwitch() | ||
} | ||
|
||
private fun initSetting() { | ||
val settingsButton = findViewById<ImageView>(R.id.settings_button) | ||
settingsButton.setOnClickListener { | ||
val intent = Intent(applicationContext, CameraSettingsActivity::class.java) | ||
intent.putExtra( | ||
CameraSettingsActivity.EXTRA_LAUNCH_SOURCE, | ||
CameraSettingsActivity.LaunchSource.LIVE_PREVIEW | ||
) | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
private fun handleCameraSwitch() { | ||
val facingSwitch = findViewById<ToggleButton>(R.id.facing_switch) | ||
facingSwitch.setOnCheckedChangeListener(this) | ||
} | ||
|
||
|
||
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) { | ||
Log.d(TAG, "Set facing") | ||
if (cameraSource != null) { | ||
if (isChecked) { | ||
cameraSource?.setFacing(CameraSource.CAMERA_FACING_FRONT) | ||
} else { | ||
cameraSource?.setFacing(CameraSource.CAMERA_FACING_BACK) | ||
} | ||
} | ||
preview?.stop() | ||
startCameraSource() | ||
} | ||
|
||
private fun createCameraSource(model: String) { | ||
if (cameraSource == null) { | ||
cameraSource = CameraSource(this, graphicOverlay) | ||
} | ||
try { | ||
when (model) { | ||
|
||
POSE_DETECTION -> { | ||
val poseDetectorOptions = | ||
PreferenceUtils.getPoseDetectorOptionsForLivePreview(this) | ||
Log.i(TAG, "Using Pose Detector with options $poseDetectorOptions") | ||
val shouldShowInFrameLikelihood = | ||
PreferenceUtils.shouldShowPoseDetectionInFrameLikelihoodLivePreview(this) | ||
val visualizeZ = PreferenceUtils.shouldPoseDetectionVisualizeZ(this) | ||
val rescaleZ = PreferenceUtils.shouldPoseDetectionRescaleZForVisualization(this) | ||
val runClassification = | ||
true /*PreferenceUtils.shouldPoseDetectionRunClassification(this)*/ | ||
cameraSource!!.setMachineLearningFrameProcessor( | ||
PoseDetectorProcessor( | ||
this, | ||
poseDetectorOptions, | ||
shouldShowInFrameLikelihood, | ||
visualizeZ, | ||
rescaleZ, | ||
runClassification, | ||
true | ||
) | ||
) | ||
} | ||
|
||
else -> Log.e(TAG, "Unknown model: $model") | ||
} | ||
} catch (e: Exception) { | ||
Log.e(TAG, "Can not create image processor: $model", e) | ||
Toast.makeText( | ||
applicationContext, | ||
"Can not create image processor: " + e.message, | ||
Toast.LENGTH_LONG | ||
).show() | ||
} | ||
} | ||
|
||
private fun startCameraSource() { | ||
if (cameraSource != null) { | ||
try { | ||
if (preview == null) { | ||
Log.d(TAG, "resume: Preview is null") | ||
} | ||
if (graphicOverlay == null) { | ||
Log.d(TAG, "resume: graphOverlay is null") | ||
} | ||
preview!!.start(cameraSource, graphicOverlay) | ||
} catch (e: IOException) { | ||
Log.e(TAG, "Unable to start camera source.", e) | ||
cameraSource!!.release() | ||
cameraSource = null | ||
} | ||
} | ||
} | ||
|
||
public override fun onResume() { | ||
super.onResume() | ||
Log.d(TAG, "onResume") | ||
createCameraSource(POSE_DETECTION) | ||
startCameraSource() | ||
} | ||
|
||
override fun onPause() { | ||
super.onPause() | ||
preview?.stop() | ||
// TODO check that | ||
cameraSource?.release() | ||
} | ||
|
||
override fun onStop() { | ||
super.onStop() | ||
cameraSource?.release() | ||
preview?.stop() | ||
|
||
} | ||
|
||
public override fun onDestroy() { | ||
super.onDestroy() | ||
if (cameraSource != null) { | ||
cameraSource?.release() | ||
} | ||
} | ||
|
||
|
||
companion object { | ||
private const val POSE_DETECTION = "Pose Detection" | ||
private const val TAG = "LivePreviewActivity" | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
app/src/main/java/com/modarb/android/posedetection/CameraPreferenceFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
|
||
package com.modarb.android.posedetection; | ||
|
||
import android.hardware.Camera; | ||
import android.os.Bundle; | ||
import android.preference.ListPreference; | ||
import android.preference.PreferenceCategory; | ||
import android.preference.PreferenceFragment; | ||
|
||
import androidx.annotation.StringRes; | ||
|
||
import com.modarb.android.R; | ||
import com.modarb.android.posedetection.Utils.CameraSource; | ||
import com.modarb.android.posedetection.Utils.PreferenceUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CameraPreferenceFragment extends PreferenceFragment { | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
addPreferencesFromResource(R.xml.pref_camera_view); | ||
setUpCameraPreferences(); | ||
} | ||
|
||
void setUpCameraPreferences() { | ||
PreferenceCategory cameraPreference = | ||
(PreferenceCategory) findPreference(getString(R.string.pref_category_key_camera)); | ||
cameraPreference.removePreference( | ||
findPreference(getString(R.string.pref_key_camerax_rear_camera_target_resolution))); | ||
cameraPreference.removePreference( | ||
findPreference(getString(R.string.pref_key_camerax_front_camera_target_resolution))); | ||
setUpCameraPreviewSizePreference( | ||
R.string.pref_key_rear_camera_preview_size, | ||
R.string.pref_key_rear_camera_picture_size, | ||
CameraSource.CAMERA_FACING_BACK); | ||
setUpCameraPreviewSizePreference( | ||
R.string.pref_key_front_camera_preview_size, | ||
R.string.pref_key_front_camera_picture_size, | ||
CameraSource.CAMERA_FACING_FRONT); | ||
} | ||
|
||
private void setUpCameraPreviewSizePreference( | ||
@StringRes int previewSizePrefKeyId, @StringRes int pictureSizePrefKeyId, int cameraId) { | ||
ListPreference previewSizePreference = | ||
(ListPreference) findPreference(getString(previewSizePrefKeyId)); | ||
|
||
Camera camera = null; | ||
try { | ||
camera = Camera.open(cameraId); | ||
|
||
List<CameraSource.SizePair> previewSizeList = CameraSource.generateValidPreviewSizeList(camera); | ||
String[] previewSizeStringValues = new String[previewSizeList.size()]; | ||
Map<String, String> previewToPictureSizeStringMap = new HashMap<>(); | ||
for (int i = 0; i < previewSizeList.size(); i++) { | ||
CameraSource.SizePair sizePair = previewSizeList.get(i); | ||
previewSizeStringValues[i] = sizePair.preview.toString(); | ||
if (sizePair.picture != null) { | ||
previewToPictureSizeStringMap.put( | ||
sizePair.preview.toString(), sizePair.picture.toString()); | ||
} | ||
} | ||
previewSizePreference.setEntries(previewSizeStringValues); | ||
previewSizePreference.setEntryValues(previewSizeStringValues); | ||
|
||
if (previewSizePreference.getEntry() == null) { | ||
CameraSource.SizePair sizePair = | ||
CameraSource.selectSizePair( | ||
camera, | ||
CameraSource.DEFAULT_REQUESTED_CAMERA_PREVIEW_WIDTH, | ||
CameraSource.DEFAULT_REQUESTED_CAMERA_PREVIEW_HEIGHT); | ||
String previewSizeString = sizePair.preview.toString(); | ||
previewSizePreference.setValue(previewSizeString); | ||
previewSizePreference.setSummary(previewSizeString); | ||
PreferenceUtils.saveString( | ||
getActivity(), | ||
pictureSizePrefKeyId, | ||
sizePair.picture != null ? sizePair.picture.toString() : null); | ||
} else { | ||
previewSizePreference.setSummary(previewSizePreference.getEntry()); | ||
} | ||
|
||
previewSizePreference.setOnPreferenceChangeListener( | ||
(preference, newValue) -> { | ||
String newPreviewSizeStringValue = (String) newValue; | ||
previewSizePreference.setSummary(newPreviewSizeStringValue); | ||
PreferenceUtils.saveString( | ||
getActivity(), | ||
pictureSizePrefKeyId, | ||
previewToPictureSizeStringMap.get(newPreviewSizeStringValue)); | ||
return true; | ||
}); | ||
} catch (RuntimeException e) { | ||
((PreferenceCategory) findPreference(getString(R.string.pref_category_key_camera))) | ||
.removePreference(previewSizePreference); | ||
} finally { | ||
if (camera != null) { | ||
camera.release(); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.